09.9.2009

LINUX: delete files before a certain date

Every now and then you’ll find yourself in a bind where you need to delete files older than a certain date. This isn’t a bid deal in small numbers, but when faced with a directory containing 120,000+ files it can be overwhelming. Not to worry! The following command will do this for you. You will simply need to adjust the date depending on what you want to do. Here is the command:

Code:

ls -la | grep May | /usr/bin/tr -s ' ' | /usr/bin/cut -d ' ' -f9 | xargs rm

That will list all files from May of the current year and delete them. Remember, Linux will not show the year of the file if it is the current year. You can use this command 12 times (once per month) and delete all files from every month. The following will list all files from 2008 and delete them:

Code:

ls -la | grep 2008 | /usr/bin/tr -s ' ' | /usr/bin/cut -d ' ' -f9 | xargs rm

Be careful using these commands as their is no way to “undelete” files in Linux. You would need to restore from a backup.

Blog Hosting By Orlando Tech Works