We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,477 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Korn Shell Script for deleting files older than 2 months

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.

2
Contributors
7
Replies
1 Day
Discussion Span
6 Years Ago
Last Updated
8
Views
abhilashnair
Newbie Poster
5 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

masijade
Industrious Poster
Team Colleague
4,253 posts since Feb 2006
Reputation Points: 1,484
Solved Threads: 494
Skill Endorsements: 17

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'

abhilashnair
Newbie Poster
5 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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 {} \;

masijade
Industrious Poster
Team Colleague
4,253 posts since Feb 2006
Reputation Points: 1,484
Solved Threads: 494
Skill Endorsements: 17

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?

abhilashnair
Newbie Poster
5 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

abhilashnair
Newbie Poster
5 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

masijade
Industrious Poster
Team Colleague
4,253 posts since Feb 2006
Reputation Points: 1,484
Solved Threads: 494
Skill Endorsements: 17

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......

abhilashnair
Newbie Poster
5 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0773 seconds using 2.68MB