•
•
•
•
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
![]() |
•
•
Join Date: Apr 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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.
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
----------------------------------------------
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
•
•
Join Date: Apr 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 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.
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
\;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
----------------------------------------------
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
•
•
Join Date: Apr 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 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 {} \;
I typed the above command as it is. It returned nothing. Do we need to give any parameters inside the brace brackets?
•
•
Join Date: Apr 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 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
Last edited by abhilashnair : Apr 26th, 2007 at 3:22 am.
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
----------------------------------------------
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
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Shell Scripting Marketplace
Other Threads in the Shell Scripting Forum
- Previous Thread: Printing file access permission....
- Next Thread: looking for....



Linear Mode