Delete Files Older Than ‘x’ Days on Linux

The find utility on linux allows you to pass in a bunch of interesting arguments, including one to execute another command on each file. We’ll use this in order to figure out what files are older than a certain number of days, and then use the rm command to delete them.

find /path/to/files* -mtime +5 -exec rm {} \;

Explanation

- The first argument is the path to the files. This can be a path, a directory, or a wildcard as in the example above. I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results.

- The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +5, it will find files older than 5 days.

- The third argument, -exec, allows you to pass in a command such as rm. The {} \; at the end is required to end the command.

Adding -f forces the delete:
i.e. find /path/to/files* -mtime +5 -exec rm -f {} \;

Moving instead of deleting:
i.e. find /path/to/files/* -mtime +30 -exec mv {} /path/to/newfolder/ \;

KiRe
Monkeynux~

About these ads

0 Responses to “Delete Files Older Than ‘x’ Days on Linux”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s





Follow

Get every new post delivered to your Inbox.

%d bloggers like this: