User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 397,615 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,499 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser:
Views: 8868 | Replies: 7
Reply
Join Date: Apr 2007
Posts: 5
Reputation: abhilashnair is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
abhilashnair abhilashnair is offline Offline
Newbie Poster

Korn Shell Script for deleting files older than 2 months

  #1  
Apr 25th, 2007
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 1,363
Reputation: masijade is a jewel in the rough masijade is a jewel in the rough masijade is a jewel in the rough masijade is a jewel in the rough 
Rep Power: 8
Solved Threads: 117
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: Korn Shell Script for deleting files older than 2 months

  #2  
Apr 25th, 2007
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.
Last edited by masijade : Apr 25th, 2007 at 9:12 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Apr 2007
Posts: 5
Reputation: abhilashnair is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
abhilashnair abhilashnair is offline Offline
Newbie Poster

Re: Korn Shell Script for deleting files older than 2 months

  #3  
Apr 25th, 2007
Originally Posted by masijade View Post
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'
Reply With Quote  
Join Date: Feb 2006
Posts: 1,363
Reputation: masijade is a jewel in the rough masijade is a jewel in the rough masijade is a jewel in the rough masijade is a jewel in the rough 
Rep Power: 8
Solved Threads: 117
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: Korn Shell Script for deleting files older than 2 months

  #4  
Apr 25th, 2007
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 {} \;
Last edited by masijade : Apr 25th, 2007 at 3:10 pm.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Apr 2007
Posts: 5
Reputation: abhilashnair is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
abhilashnair abhilashnair is offline Offline
Newbie Poster

Re: Korn Shell Script for deleting files older than 2 months

  #5  
Apr 26th, 2007
Originally Posted by masijade View Post
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?
Reply With Quote  
Join Date: Apr 2007
Posts: 5
Reputation: abhilashnair is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
abhilashnair abhilashnair is offline Offline
Newbie Poster

Re: Korn Shell Script for deleting files older than 2 months

  #6  
Apr 26th, 2007
Originally Posted by abhilashnair View Post
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
Last edited by abhilashnair : Apr 26th, 2007 at 3:22 am.
Reply With Quote  
Join Date: Feb 2006
Posts: 1,363
Reputation: masijade is a jewel in the rough masijade is a jewel in the rough masijade is a jewel in the rough masijade is a jewel in the rough 
Rep Power: 8
Solved Threads: 117
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: Korn Shell Script for deleting files older than 2 months

  #7  
Apr 26th, 2007
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.
Last edited by masijade : Apr 26th, 2007 at 4:58 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Apr 2007
Posts: 5
Reputation: abhilashnair is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
abhilashnair abhilashnair is offline Offline
Newbie Poster

Re: Korn Shell Script for deleting files older than 2 months

  #8  
Apr 26th, 2007
Originally Posted by masijade View Post
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......
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Shell Scripting Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 8:01 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC