LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   shell script to remove old files based on date (https://www.linuxquestions.org/questions/linux-general-1/shell-script-to-remove-old-files-based-on-date-7368/)

WindozBytes 10-08-2001 09:39 AM

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?

d3funct 10-08-2001 01:35 PM

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.

WindozBytes 10-09-2001 06:36 AM

d3funct:

Thanks for the input and for the caveats. This looks like exactly what I was looking for. Really clever signature, by the way.

dragon49 02-27-2003 12:19 AM

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.

oxedos 03-29-2009 01:44 AM

Dear d3funct, i really want to thank you for the great and simple way to deal with the files according to their modification date.

chrism01 03-30-2009 08:33 PM

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

jdaw 07-29-2009 02:47 PM

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

catkin 07-29-2009 02:55 PM

Hello dragon49 :)
Quote:

Originally Posted by dragon49 (Post 234459)
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.

Are you asking about the scheduling (see the crontab man page and ask again if that doesn't tell you what you need to know) or about deleting the files? If the script runs at 12 AM and you want to delete all files created before that time then rm /my_directory/* will do the job (unless you have "hidden" files starting with a ".").

Best

Charles

chandan_raka 09-23-2009 02:46 AM

hi,

how to get to know the oldest file in the perticulat dir?

catkin 09-23-2009 03:30 AM

Quote:

Originally Posted by chandan_raka (Post 3693843)
hi,

how to get to know the oldest file in the perticulat dir?

Hello Chandan :)

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.

michelek 05-29-2012 05:37 AM

oldest file in dir
 
Hello Chandan

ls -t1 pathname | head -1

sanpraja 06-04-2012 01:16 AM

Quote:

Originally Posted by dragon49 (Post 234459)
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.



------------
create a crontab file using "crontab -e" & and "crontab -l" to list it.
place the line -
*/59 */11 * * * /script_path arg1 arg2. as required.

evo2 06-04-2012 01:21 AM

Ahh!!!! Multiple resurrections! Zombie thread! Run for the hills!


All times are GMT -5. The time now is 03:11 PM.