, ,

Unix – Delete files older than a certain number of days using find command

Delete files older than 30 days

find . -mtime +30 -exec rm {} \;

1. Save the deleted files to a log file

find /home/a -mtime +5 -exec ls -l {} \; > mylogfile.log

This way, you’ll get a line at top with the date the line is executed (also a line at the bottom).
The last two semicolons are necessary, one is for find and one for bash.

2. modified

Find and delete files modified in the last 30 minutes


find /tmp/ -type f -mmin 30 -exec rm {} \;

mtime = days
mmin = minutes

3. force

force delete temp files older then 30 days

find /tmp -mtime +30 -exec rm -f {} \;

4. move the files

move files older than 30 days to an archive folder – and preserve path strcuture

find /tmp -mtime +30 -exec mv -t {} /archive/directory/ \;

mv -t: ensure directory structure is preserved

References

1. Source of Tips: http://www.howtogeek.com/howto/ubuntu/delete-files-older-than-x-days-on-linux/
2. Using the find command to find Files by date: unix-find-newest-files-and-files-by-timestamp
3. man find
4. man mv
5. Find Files By Age and Timestamp: /2383/unix-find-files-by-age-and-timestamp


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *