Quickie: Deleting Old Files

Removing old files from Linux the fun way

All Articles

Tags: quickie, linux

Many people have the need to delete files older than a certain range. For instance, you may want to remove all podcasts that are older than a certain time period or delete other files that you receive regularly. Making use of the find command and mtime, you are able to delete them with ease.


find . -name *.ogg -mtime +7 -exec rm {} \;

The above one-liner removes all my ogg-based media files in my current directory that are more than a week old. It is best to use absolute paths -- ESPECIALLY IF RUNNING BY CRON. So the cron-safe code looks like this:


find /home/steven/music/ -name *.ogg -mtime +7 -exec rm {} \;

You can also use this with other command such as moving the file, batch renaming them (if you want all older files to have a .bak extension or whatnot), as well as many other uses. To move the files, simply:


find /home/steven/feeds/ -name *.csv -mtime +30 -exec mv {} /home/steven/feeds/old \;

With enough MAN pages and will, you can do all kinds of cool stuff.