I need to write a shell script that will filter out all files in a directory that are more than two months old and then remove those files.

Example: Suppose I am running this script on May 1st 2007. It should list out all files which are created before Mar 1st 2007(i.e. more than 2 month old). Then it should remove all those listed files, which means, after this script runs, the directory should not contain files created before March 1st.

find /path/to/directory -type f -mtime 61 -exec rm -f {} \; 61 (days) works for everyting except it is one day short for July August and 1 to two days long for (January/February) / (February/March).

Edit: make it 62 if it absolutely must be every last day for two months. Retaining too much is usually better than retaining too little.

find /path/to/directory -type f -mtime 61 -exec rm -f {} \; 61 (days) works for everyting except it is one day short for July August and 1 to two days long for (January/February) / (February/March).

Edit: make it 62 if it absolutely must be every last day for two months. Retaining too much is usually better than retaining too little.

Thanks for the quick reply. I tried the command

find -type f -mtime 61 -exec ls -ltr

I expected that it would list out all files more than 61 days old. But it returned the following error

find: missing argument to `-exec'

The exec portion of the find command must be ended with the two literal characters \; And before this you must provide the argument to ls. The argument is the file found and you pass it the command with the two literal characters {} So your complete command will be find -type f -mtime 61 -exec ls -ltr {} \;

The exec portion of the find command must be ended with the two literal characters \; And before this you must provide the argument to ls. The argument is the file found and you pass it the command with the two literal characters {} So your complete command will be find -type f -mtime 61 -exec ls -ltr {} \;

I typed the above command as it is. It returned nothing. Do we need to give any parameters inside the brace brackets?

I typed the above command as it is. It returned nothing. Do we need to give any parameters inside the brace brackets?

Now it is working. But one other issue is that this command will return only those files which match the exact date. For example if we run the command on April 26, it will list out only those files created on Feb 26. If there is no file with that particular date, this command will return nothing. But my requirement is all files created on or before two months

Change the 61 to +61 for all files more than 61 days old or to -61 for all files less than 61 days old (Both not including exactly 61 days old). 61 (without a + or -) means eactly 61 days old.

Change the 61 to +61 for all files more than 61 days old or to -61 for all files less than 61 days old (Both not including exactly 61 days old). 61 (without a + or -) means eactly 61 days old.

Great.. That solves my problem Thanks......

I want to do the same thing as above, but with Directories, not files. I'm guessing i just change the -type f to -type d?

This article has been dead for over six months. Start a new discussion instead.