Friday 26 June 2015

Delete files that are x days old

Sometimes in Linux, you want to clear out older files in a directory. One instance would be if you have a security system and it continuously writes video files to a directory on your NAS (Network Attached Storage) until it fills it up. You’ve figured out that if you keep a week’s worth of video, it will usually leave plenty of space for other users.

What I would suggest here is creating a cron job that runs every night and runs something like the following command:
What it all means:
find: the command that will search for the files
/path/to/files/: the top level directory to start searching
-type f: so we don’t remove directories, only files
-mtime +7: files older than ‘7’ days. Change to ‘+14′ to delete files older than 2 weeks.
-exec: what to do with the files we find
rm -rf: remove them recursively, force
{}: this represents each file we find
\;: the end of the exec
SO – the crontab entry would be this:
This will run every night at 2am.

1 comment:

  1. IBM Websphere Training | 21cssindia
    www.21cssindia.com/IBM-Websphere-Training
    IBM Websphere Training Click Here For Enquiry Basics and new features.

    ReplyDelete