Find files modified before 15 days in current directory and delete them.

In this article, we will see how to find files/directory older than say 15 days and delete those files.

The tip is very useful and we use this every alternate day to clean up required files or to do housekeeping.

Lets have a look at the command.

Find and Delete Old Files which are not modified from last 15 days

[root@ngelinux01 log]# find . -mtime +15 -print | xargs rm -f {}\;
rm: cannot remove ‘./sssd’: Is a directory
rm: cannot remove ‘./ppp’: Is a directory
rm: cannot remove ‘./ntpstats’: Is a directory
rm: cannot remove ‘./pluto’: Is a directory
rm: cannot remove ‘./pluto/peer’: Is a directory

If you want to remove directories also, then you can add “-r” in the options of rm command.

Understanding the Command
1. Finding the files older than 15 days.

# find . -mtime +15 -print

Here mtime looks for files which are not modified from last 15 days, and print those results.

2. Removing the searched files

# xargs rm -f {}

xargs processes each searched result and pass it with “rm -f {filename}”

Leave a Reply

Your email address will not be published.