![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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 {} \;
|
|
#3
|
|||
|
|||
|
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 mtime is initialised with the date the file was created. |
|
#4
|
||||
|
||||
|
Quote:
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 ... |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
||||
|
||||
|
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. |
|
#7
|
|||
|
|||
|
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.
|
|||
| Google The UNIX and Linux Forums |