Traversing thru dirs and deleting files based on date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Traversing thru dirs and deleting files based on date
# 1  
Old 07-17-2007
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  
Old 07-17-2007
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  
Old 07-18-2007
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  
Old 07-18-2007
With zsh:

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

# 5  
Old 07-18-2007
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  
Old 07-18-2007
Quote:
Originally Posted by ravi2082
[...]
Some filenames have spaces and this is causing an error while deleting.
Any way to rectify this ?
Yes, use my zsh example Smilie
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..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find the count of files by last created date based on the given date range

My unix version is IBM AIX Version 6.1 I tried google my requirement and found the below answer, find . -newermt “2012-06-15 08:13" ! -newermt “2012-06-15 18:20" But newer command is not working in AIX version 6.1 unix I have given my requirement below: Input: atr files: ... (1 Reply)
Discussion started by: yuvaa27
1 Replies

2. UNIX for Dummies Questions & Answers

Looking for command line to find dirs based on size and date

Hi, My first time on this site, please excuse me if I've come to the wrong forum. I'm fairly new to Unix/Linux and hoping you can help me out. I'm looking for a command line that will return a list of directories that are larger than 50M and older than 2 days. I thought it may be... (6 Replies)
Discussion started by: Wisconsingal
6 Replies

3. Shell Programming and Scripting

Deleting lines based on a condition for a group of files

hi i have a set of similar files. i want to delete lines until certain pattern appears in those files. for a single file the following command can be used but i want to do it for all the files at a time since the number is in thousands. awk '/PATTERN/{i++}i' file (6 Replies)
Discussion started by: anurupa777
6 Replies

4. UNIX for Dummies Questions & Answers

Deleting files based on Substring match

In folder there are files (eg ABS_18APR2012_XYZ.csv DSE_17APR2012_ABE.csv) . My requirement is to delete all the files except today's timestamp I tried doing this to list all the files not having today's date timestamp #!/bin/ksh DATE=`date +"%d%h%Y"` DIR=/data/rfs/... (9 Replies)
Discussion started by: manushi88
9 Replies

5. UNIX for Dummies Questions & Answers

deleting files based on the suffix

how do we delete files based on the suffix??? (1 Reply)
Discussion started by: saggiboy10
1 Replies

6. Shell Programming and Scripting

Deleting files based on their size

I have several files in a folder and I would like to delete the ones that do not contain all the required information (size) let say 1kb. Any ideas? (4 Replies)
Discussion started by: Xterra
4 Replies

7. Shell Programming and Scripting

Deleting the files based on the date's

I have to write one script which will delete the files in the below passion. If today is 17-Feb-2010 then the script delete only 17-JAN-2010 files from the directory. Could you please help me, How will I delete the files when the year is leap year, if today is 30th Mar 2010 then how will... (1 Reply)
Discussion started by: kandi.reddy
1 Replies

8. UNIX for Dummies Questions & Answers

deleting files based on file name and modified time

Hi, I have some log files created in the following fashion Ex: file name modified date 1) s.log1 01-jan-08 2) s.log2 02-jan-08 3) s.log3 03-jan-08 4) s.log4 04-jan-08 Now I want to have the latest 2 logs and delete the others. Can you tell me the one liner /... (1 Reply)
Discussion started by: ammu
1 Replies

9. Shell Programming and Scripting

command for deleting log files based on some condition

Hello, Can anyone pls. provide me with the command for deleting files older then 15 days with a restriction to keep at least 5 files in a directory even if they are older then 15 days. Any help will be highly appreciated. Thanks, Pulkit (4 Replies)
Discussion started by: pulkit
4 Replies

10. Programming

deleting files on particular date

Hello, In unix what is the command to delete files created on a particular date. for example: I have a list of files in a directory Nov 8 08:30 abc Nov 8 17:00 xyz Nov 9 12:30 test Nov 9 14:45 test2 Nov 9 18:15 quick Nov 10 07:20 quick2 Nov 11 19:00 quick3 Now i would like to delete... (1 Reply)
Discussion started by: jazz
1 Replies
Login or Register to Ask a Question