Find and delete files older than X days in Unix

I wrote about setting up a cron job on my work machine to process some files in a shared Dropbox folder.

After a while, old files will start to accumulate there and I’d want to delete them, by adding another command to the crontab.

The criteria for deleting files in my case are:
– File is older than 5 days
– File has an extension .csv (keep other files like readme)
– Do not delete a few example .csv files

So HGH the HGH Unix command will look like a pipe that first finds the files, then deletes them.

find  X -exec rm {} ;

Tip from howtogeek.com

It’s pretty easy to find all .csv files older than 5 days:

find /path/to/files/*.csv -mtime +5

And then, to exclude certain CSV files that I want to keep, I used -not and -and operators to specify filenames (test.csv, test_out.csv and input_example.csv):

find /path/to/files/*.csv 
-not -iname test* -and -not -iname input*  -mtime +5

Tip from linux.ie

Advertisements

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 )

Google+ photo

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

Connecting to %s