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
deleting files based on file name and modified time ammu UNIX for Dummies Questions & Answers 1 01-22-2008 07:09 AM
command for deleting log files based on some condition pulkit Shell Programming and Scripting 4 01-09-2008 02:17 AM
Count of files based on date? sbasetty Shell Programming and Scripting 6 01-11-2007 11:02 AM
deleting files on particular date jazz High Level Programming 1 11-24-2005 07:45 AM
Deleting files older than a given date rajugp1 Shell Programming and Scripting 3 12-09-2002 10:08 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-17-2007
Registered User
 

Join Date: Jul 2007
Posts: 2
Traversing thru dirs and deleting files based on date

Hi Folks

I am pretty new to unix and shellscripting.
I need help on writing logic on traversing recursively through a set of directories under a top-level folder and delete files(mostly text) which are 1 month old.
Can you people help me on this?

Thanks a lot
Ravi

Last edited by ravi2082; 07-17-2007 at 09:07 AM. Reason: missed out some words
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-17-2007
Registered User
 

Join Date: Sep 2001
Posts: 20
This should do what you're looking for:

DAYS=30

echo 'Daily cleanup of backup directories more than' $DAYS 'days old'

if [ -d /wasdata ]
then

FILES=`find /wasdata2 -type f -mtime +$DAYS`

for f in $FILES
do
echo 'Deleting' $f
rm $f
done

fi
Reply With Quote
  #3 (permalink)  
Old 07-18-2007
Registered User
 

Join Date: May 2007
Posts: 211
You can do the same in a single command and log it too using the tee command or redirecting the output to a file.

Code:
find /your_path -type f -mtime +30 -exec rm -f {} \; | tee delete.log
Reply With Quote
  #4 (permalink)  
Old 07-18-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,432
With zsh:

Code:
rm -- **/*(.m+30)
Reply With Quote
  #5 (permalink)  
Old 07-18-2007
Registered User
 

Join Date: Jul 2007
Posts: 2
FILES=`find /somedir -type f -mtime +$DAYS`

for f in $FILES
do
rm $f

done


Some filenames have spaces and this is causing an error while deleting.
Any way to rectify this ?

Thanks
Ravi
Reply With Quote
  #6 (permalink)  
Old 07-18-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,432
Quote:
Originally Posted by ravi2082 View Post
[...]
Some filenames have spaces and this is causing an error while deleting.
Any way to rectify this ?
Yes, use my zsh example
Or:

- if your find/xargs have the print0/-0 option, you could:

Code:
find /somedir -type f -mtime +$DAYS -print0|xargs -0 rm
- otherwise:

Code:
find /somedir -type f -mtime +$DAYS|while IFS= read;do rm -- "$REPLY";done
or (if your find supports it):

Code:
find /somedir -type f -mtime +$DAYS -exec rm {} +

Last edited by radoulov; 07-18-2007 at 01:35 PM.
Reply With Quote
Google UNIX.COM
Reply

Tags
mtime

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:32 PM.


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

Content Relevant URLs by vBSEO 3.2.0