shell script to remove old files based on date
I have a cron job which tars/compresses various files on a daily basis. So that I don't eventually run out of space, what I need to do now is write a shell script to remove files in a specified directory which are older than, say, 60 days. Does anyone have a simple way to do this? Or, can someone point me in the right direction?
|
Look at the manpage for the "find" command. Here is an example of what you want.
find /var/log -mtime +60 -type f -exec rm -rf {} \; This command will do a search in /var/log for all files that were last modified 60 or more days ago and executes a recursive forced (-rf) remove (rm). The "{}" (curly braces) is the place holder for exec to use where it will put the name of the file, and the "\;" tells exec that's the end of the statement. Find is very powerful, and I suggest you do some reading BEFORE you do any removing using "find". Also, as a test you can replace the "rm -rf" with "ls -la" to get a list of all the files that would be removed. And, if you want to remove files with specific names or extensions use the "-name" argument. |
d3funct:
Thanks for the input and for the caveats. This looks like exactly what I was looking for. Really clever signature, by the way. |
I have a similar issue. I need to schedule a job, but can't figure out the syntax. I want a job to run daily at 12:00 a.m. The job will delete all files in a certain directory that were created prior to 12:00 a.m.
What is the syntax to accomplish this task? I use a Bourne shell. Thank you. |
Dear d3funct, i really want to thank you for the great and simple way to deal with the files according to their modification date.
|
@WindozBytes: have you looked into logrotate, it does exactly that sort of thing.
Its part of the std install in linux, look in /etc for logrotate.conf, logrotate.d & man logrotate. |
Date created -vs- modified date.
I've got the very same script running in a cron job, but since nothing is 'modifying' the log files, they don't get deleted. I've tried using -ctime but having the same results. Is there a switch or some kind of command that will sort (delete) based on raw age of the file?
Thanks, Jake |
Hello dragon49 :)
Quote:
Best Charles |
hi,
how to get to know the oldest file in the perticulat dir? |
Quote:
Better start your own thread, especially as your question is not related to the subject of this thread. EDIT: sorry, it is kind of related, but still better to start your own thread asking your specific question. |
oldest file in dir
Hello Chandan
ls -t1 pathname | head -1 |
Quote:
------------ create a crontab file using "crontab -e" & and "crontab -l" to list it. place the line - */59 */11 * * * /script_path arg1 arg2. as required. |
Ahh!!!! Multiple resurrections! Zombie thread! Run for the hills!
|
All times are GMT -5. The time now is 03:11 PM. |