Bash - find files older than X minutes and move them
If anyone else needs it:
find DIR_A -maxdepth 1 -type f -mmin +10 -exec mv "{}" DIR_B/ \
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"
find -mtime files older than 1 hour
What about -mmin
?
find /var/www/html/audio -daystart -maxdepth 1 -mmin +59 -type f -name "*.mp3" \
-exec rm -f {} \;
From man find:
-mmin n
File's data was last modified n minutes ago.
Also, make sure to test this first!
... -exec echo rm -f '{}' \;
^^^^ Add the 'echo' so you just see the commands that are going to get
run instead of actual trying them first.
Delete files older than X minutes
When used with -mmin
, -daystart
appears to make it calculate from the end of today, not the beginning.
If you just want to find files modified more than 59 minutes ago, you don't need that option. -mmin
calculates from the current time by default.
barmar@dev:~/testdir$ date
Sat Jul 20 10:02:20 CDT 2013
barmar@dev:~/testdir$ ls -l
total 0
-rw-r--r-- 1 barmar adm 0 Jul 20 09:57 a.txt
barmar@dev:~/testdir$ find . -maxdepth 1 -mmin +2 -type f
./a.txt
barmar@dev:~/testdir$ find . -maxdepth 1 -mmin +10 -type f
How to delete files older than X hours
Does your find
have the -mmin
option? That can let you test the number of mins since last modification:
find $LOCATION -name $REQUIRED_FILES -type f -mmin +360 -delete
Or maybe look at using tmpwatch
to do the same job. phjr also recommended tmpreaper
in the comments.
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.
Related Topics
Passing Main Script Variables into Perl Modules
Learning Kernel Hacking and Embedded Development at Home
How to Make Vim Play Typewriter Sound When I Write a Letter
How to Load Luks Passphrase from Usb, Falling Back to Keyboard
How to Use Curl in a Shell Script
Differencebetween Nptl and Posix Threads
Why Do I Get 'Permission Denied' After Using ./File2 in Linux
In Unix, How to Run 'Make' in a Directory Without Cd'Ing to That Directory First
Best Practices for Git Repositories on Open Source Projects
How to Extract Files Without Folder Structure Using Tar
Linux, Why Can't I Write Even Though I Have Group Permissions
Check If File Exists on Remote Host with Ssh
Does Epoll(), Do Its Job in O(1)
Tool to Visualize the Device Tree File (Dtb) Used by the Linux Kernel