LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 10-08-2001, 10:39 AM   #1
WindozBytes
Member
 
Registered: Aug 2001
Location: CT, USA
Distribution: Mandriva 2008
Posts: 105

Rep: Reputation: 15
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?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-08-2001, 02:35 PM   #2
d3funct
Member
 
Registered: Jun 2001
Location: Centralia, WA
Posts: 274

Rep: Reputation: 31
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.
 
2 members found this post helpful.
Old 10-09-2001, 07:36 AM   #3
WindozBytes
Member
 
Registered: Aug 2001
Location: CT, USA
Distribution: Mandriva 2008
Posts: 105

Original Poster
Rep: Reputation: 15
d3funct:

Thanks for the input and for the caveats. This looks like exactly what I was looking for. Really clever signature, by the way.
 
Old 02-27-2003, 01:19 AM   #4
dragon49
LQ Newbie
 
Registered: Feb 2003
Distribution: Caldera Open Linux 3.1
Posts: 3

Rep: Reputation: 0
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.
 
Old 03-29-2009, 02:44 AM   #5
oxedos
LQ Newbie
 
Registered: Mar 2009
Posts: 1

Rep: Reputation: 0
Dear d3funct, i really want to thank you for the great and simple way to deal with the files according to their modification date.
 
Old 03-30-2009, 09:33 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
@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.
 
Old 07-29-2009, 03:47 PM   #7
jdaw
LQ Newbie
 
Registered: Sep 2008
Posts: 3

Rep: Reputation: 0
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
 
Old 07-29-2009, 03:55 PM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Hello dragon49
Quote:
Originally Posted by dragon49 View Post
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
 
Old 09-23-2009, 03:46 AM   #9
chandan_raka
Member
 
Registered: Apr 2005
Location: BC
Distribution: Centos
Posts: 34

Rep: Reputation: 16
hi,

how to get to know the oldest file in the perticulat dir?
 
Old 09-23-2009, 04:30 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Quote:
Originally Posted by chandan_raka View Post
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.

Last edited by catkin; 09-23-2009 at 04:37 AM.
 
Old 05-29-2012, 06:37 AM   #11
michelek
LQ Newbie
 
Registered: Nov 2008
Posts: 8

Rep: Reputation: 0
oldest file in dir

Hello Chandan

ls -t1 pathname | head -1
 
Old 06-04-2012, 02:16 AM   #12
sanpraja
LQ Newbie
 
Registered: May 2010
Posts: 4

Rep: Reputation: 0
Quote:
Originally Posted by dragon49 View Post
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.
 
Old 06-04-2012, 02:21 AM   #13
evo2
Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,590

Rep: Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227
Ahh!!!! Multiple resurrections! Zombie thread! Run for the hills!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to delete mail based on date gquiring Linux - General 8 05-08-2013 10:24 AM
Shell script to remove backup ~ files hallamigo Linux - General 3 09-13-2010 04:47 PM
Delete files based on date stefaandk Linux - General 3 06-17-2005 03:20 AM
Script for deleting files based on date MaverickApollo Linux - General 3 02-03-2004 08:54 PM
Listing Files based on date axero Linux - Newbie 2 10-19-2003 05:58 PM


All times are GMT -5. The time now is 08:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration