find: Remove files modified more than 31 days ago

This is one of those things that you will run into eventually and once you know how, it is extremely useful.

Example:

# find ./ -type f -mtime +31 -exec rm {} ;

This will find all of the files older than 31 days, If you want to do files and directories you will need two commands.

# find ./ -not -type d -mtime +31 -exec rm {} ;  
# find ./ -type d -mtime +31 -exec rmdir {} ;

The only reason you run the commands twice is because when find runs rm on a non empty directory it will error. This is safer than using an rm -Rf just in case find wants to delete a directory with newer files in it.

Update

I just found the below syntax while writing this, in Unix/Linux there are many ways to skin a cat.

Example:

 [[email protected] tmp]# find ./ -mtime +31  
 ./sudoers.new  
 ./file.symlink  
 ./somedirectory  
 ./play/list.txt2  
 ./tar.tgz  
 [[email protected] tmp]# find ./ -mtime +31 -delete  
 [[email protected] tmp]# find ./ -mtime +31

About Benjamin

Benjamin is a Infrastructure and Software Engineer. On this blog he writes about Linux, Docker, Programming as well as other Systems topics.

Learn more about Linux

If you liked this article, check out Benjamin's book: Red Hat Enterprise Linux Troubleshooting Guide. Where you can learn a lot more about troubleshooting Linux systems. This book is filled with tips and techniques he has learned over years of managing mission critical systems.

Related