Phil for Humanity Phil for Humanity
A Guide for the Survival of Humankind and Helping the World, Society, and Yourself.

Unix: Find Old Files

If you write code like I do, then you create a lot of log files. And after a while, a lot of old log files should be deleted. Here is how I find old files that are a certain age that can be deleted.

find PATH -type f –mtime +NUMBER -print

The "PATH" is to define the directory to search in. The "-type f" is to only find files. The "-mtime +NUMBER" is to only find files with time stamps over a certain NUMBER of days old. The "-print" is to display all found files.

For instance, assume that you want to find all the temporary files in the "/tmp” directory that are over 30 days old. You can do this by running this command:

find /tmp -type f –mtime +30 -print

Now if you want to automatically delete the old temporary files, you can do this:

find /tmp -type f –mtime +30 –print | xargs rm -f

Now, I have cron jobs to automatically clean up my log files, so I never have to manually run these commands or run out of disk space.

by Phil for Humanity
on 20161104

Related Articles
 » UNIX Support: Recursively Find
 » Wrapper Script
 » UNIX Warning Message: could not open /dev/kbd