Move Files That Are 30 Minutes Old

Move files that are 30 minutes old

You can use find along with -exec for this:-

Replace /sourcedirectory and /destination/directory/ with the source and target paths as you need.

find /sourcedirectory -maxdepth 1 -mmin -30 -type f -exec mv "{}" /destination/directory/ \;

What basically the command does is, it tries to find files in the current folder -maxdepth 1 that were last modified 30 mins ago -mmin -30 and move them to the target directory specified. If you want to use the time the file was last accessed use -amin -30.

Or if you want to find files modified within a range you can use something like -mmin 30 -mmin -35 which will get you the files modified more than 30 but less than 35 minutes ago.

References from the man page:-

   -amin n
File was last accessed n minutes ago.

-atime n
File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime
+1, a file has to have been accessed at least two days ago.

-mmin n
File's data was last modified n minutes ago.

-mtime n
File's data was last modified n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file modification times.

Move file that has aged x minutes

Checking relative age of files can be done by Bash's built-in file date comparison operator -ot.

See help test:

FILE1 -nt FILE2 True if file1 is newer than file2 (according to modification date).

FILE1 -ot FILE2 True if file1 is older than file2.

#!/usr/bin/env bash

declare -- TIME_FILE
TIME_FILE="$(mktemp)" || exit 1 # Failed to create temp-file

trap 'rm -- "$TIME_FILE"' EXIT # Purge the temp-file on exit

declare -i EXPECTED_AGE_TIME=10

# Set the time of the referrence $TIME_FILE to $EXPECTED_AGE_TIME minutes
touch --date "$((EXPECTED_AGE_TIME)) min ago" "$TIME_FILE"

# If $FILE is older than $TIME_FILE, then move it
[[ "$FILE" -ot "$TIME_FILE" ]] && mv -- "$FILE" "./loaded/$FILE"

Move files older than x minutes

You can't call methods without an object providing the method, so .MoveFile should be fso.MoveFile. However, in its current form the script would move all files from C:\CopyFlow\Directory test\Inn if any of the files in the folder passed as argument to the script is older than 5 minutes.

What you need to do is pass C:\test\inn as the argument to the script, and move only those files that actually are older:

If age > age_threshold Then
file.Move "C:\test\inn\error\"
End If


Related Topics



Leave a reply



Submit