Bash Command to Delete all Files Based on Age

To delete all files in the current directory (and all subdirectories) based on age (in this case, 7 days), you can use the following:

find `pwd -P` -mtime +7 -ctime +7 | xargs -I {} bash -c 'if [ ! -d {} ] ; then \
echo {}; fi' | xargs -I {} rm -f {}

Note that using “find” over very large file subsets can be slow.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.