The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Removing files older than 7 days texasoeb UNIX for Dummies Questions & Answers 3 04-20-2007 01:04 PM
ls latest 4 days or specify days of files in the directory happyv Shell Programming and Scripting 3 01-22-2007 04:16 AM
Searching for files over 30 days old in current directory cxredd4 Shell Programming and Scripting 18 06-10-2006 11:16 PM
How to zip a modified file 15 days before but not scanning my sub directory files skrish70 Shell Programming and Scripting 1 10-10-2005 12:41 PM
removing files that are 60 days old aojmoj Shell Programming and Scripting 5 01-26-2004 11:45 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-23-2005
Registered User
 

Join Date: Jun 2005
Location: Oklahoma
Posts: 2
Removing files automatically from a directory after 30 days.

Hello;

I have a directory that is collecting log and act files. I need to write a script that will remove these files once they are 30 days old. I have read through a number of threads on this site that have given me a great deal of information. However I have what seems to be a unique situation. I can't use the -atime or the -mtime commands because not all the files that haven't been modified or accessed can be deleted. There are non log or act files in this directory. One thing that may help is that the log and act files use yyyymmdd.log or yyyymmdd.act file naming system. IE 20050623.log or 20050623.act. How can I use this to generate my script? Thank you very much in advance for your knowledge and help.
Reply With Quote
Forum Sponsor
  #2  
Old 06-23-2005
Registered User
 

Join Date: Oct 2002
Posts: 676
Do this first to make sure it's getting what you want.

Code:
find /path/to/dir -type f -name "*.log" -a -name "*.act" -mtime +30 -exec ls -l {} \;

Then do this if it goes according to your criteria:

Code:
find /path/to/dir -type f -name "*.log" -a -name "*.act" -mtime +30 -exec rm -f {} \;
Reply With Quote
  #3  
Old 06-24-2005
Registered User
 

Join Date: Jun 2005
Location: Ireland
Posts: 61
There are a fw problems with that find.

1. "*.act and *.log" will not match anything
2. It will start an rm process per file

Something like the following would be better:

Code:
find /path/to/dir -type f \( -name "*.log" -o -name "*.act" \) -mtime +30 -print0 |
xargs -r0 rm
Note I don't understand the OPs comment about mtime not working.
Note mtime is initialised with the date the file was created.
Reply With Quote
  #4  
Old 06-24-2005
Just Ice's Avatar
Lights on, brain off.
 

Join Date: Mar 2005
Location: in front of my computer
Posts: 629
Quote:
Originally Posted by pixelbeat
There are a fw problems with that find.

1. "*.act and *.log" will not match anything
2. It will start an rm process per file

Something like the following would be better:

Code:
find /path/to/dir -type f \( -name "*.log" -o -name "*.act" \) -mtime +30 -print0 |
xargs -r0 rm
Note I don't understand the OPs comment about mtime not working.
Note mtime is initialised with the date the file was created.
mtime is changed if the file is modified after the creation date ... you could have a file that has been there 5 years but modified twice daily so it's mtime will always be "-1" ...

IMUO --- the only difference in most cases between using "|xargs" as compared to "-exec" is the use of 2 commands instead of 1 ... in a directory with a small number of files, the use of either form is dictated more by the user's preference than anything else ...
Reply With Quote
  #5  
Old 06-24-2005
Registered User
 

Join Date: Jun 2005
Location: Ireland
Posts: 61
xargs is a more general/scalable solution to many problems.

Having reread the OP, I think he meant "have" rather than "haven't" in the following:

"I can't use the -atime or the -mtime commands because not all the files that haven't been modified or accessed can be deleted."

So how about using -ctime ?
I know c means change not create, but that should
be the same.
Reply With Quote
  #6  
Old 06-24-2005
RishiPahuja's Avatar
Registered User
 

Join Date: Apr 2005
Location: Bangalore, India
Posts: 203
Thumbs up

Try this piece of code if you want to be 100% sure.

for filename in list
do
file_dt=`echo $filename | awk '{print substr($1,length($1)-12,8)}'`
today_dt=`date '+%Y%m%d'`
(( diff=$today_dt-$file_dt))

if [ $diff -gt 30 ]; then
do whatever u want
....
fi
done

using find and rm combination will be more fast.
but if you are not too sure, u can use this.
Reply With Quote
  #7  
Old 06-24-2005
Registered User
 

Join Date: Jun 2005
Location: Oklahoma
Posts: 2
Thank You

I have it working. I had to make a couple of minor tweaks but it works great now. I would like to thank everyone who replied. Everyone's responses where very clear and made this little task very easy. Again thank you all very very much.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
mtime

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:08 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0