![]() |
|
|
|
|
|||||||
| 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 |
| deleting files based on file name and modified time | ammu | UNIX for Dummies Questions & Answers | 1 | 01-22-2008 08:09 AM |
| command for deleting log files based on some condition | pulkit | Shell Programming and Scripting | 4 | 01-09-2008 03:17 AM |
| Count of files based on date? | sbasetty | Shell Programming and Scripting | 6 | 01-11-2007 12:02 PM |
| deleting files on particular date | jazz | High Level Programming | 1 | 11-24-2005 08:45 AM |
| Deleting files older than a given date | rajugp1 | Shell Programming and Scripting | 3 | 12-09-2002 11:08 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
With zsh:
Code:
rm -- **/*(.m+30) |
|
#5
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
Or: - if your find/xargs have the print0/-0 option, you could: Code:
find /somedir -type f -mtime +$DAYS -print0|xargs -0 rm Code:
find /somedir -type f -mtime +$DAYS|while IFS= read;do rm -- "$REPLY";done Code:
find /somedir -type f -mtime +$DAYS -exec rm {} +
Last edited by radoulov; 07-18-2007 at 01:35 PM. |
||||
| Google The UNIX and Linux Forums |