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 and shell scripting languages 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 11:09 AM
command for deleting log files based on some condition pulkit Shell Programming and Scripting 4 01-09-2008 06:17 AM
Count of files based on date? sbasetty Shell Programming and Scripting 6 01-11-2007 03:02 PM
deleting files on particular date jazz High Level Programming 1 11-24-2005 11:45 AM
Deleting files older than a given date rajugp1 Shell Programming and Scripting 3 12-09-2002 02:08 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-17-2007
ravi2082 ravi2082 is offline
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 01:07 PM.. Reason: missed out some words
  #2 (permalink)  
Old 07-17-2007
dsimpg1 dsimpg1 is offline
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
  #3 (permalink)  
Old 07-18-2007
lorcan lorcan is offline
Registered User
  
 

Join Date: May 2007
Posts: 219
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

  #4 (permalink)  
Old 07-18-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926
With zsh:


Code:
rm -- **/*(.m+30)

  #5 (permalink)  
Old 07-18-2007
ravi2082 ravi2082 is offline
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
  #6 (permalink)  
Old 07-18-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926
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 05:35 PM..
Closed Thread

Bookmarks

Tags
mtime

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:44 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0