Operating System - HP-UX
1847252 Members
3039 Online
110263 Solutions
New Discussion

Re: Find and remove files between two dates

 
rajasekar.m
Advisor

Find and remove files between two dates

plz send the command to find and remove files betweeen two dates, like from feb 10 to 12 in 2006.
4 REPLIES 4
Peter Nikitka
Honored Contributor

Re: Find and remove files between two dates

Hi,

please tell us your request more exactly:
- all files in a directory?
- all files recursivly under a directory tree?
- should directories an links removed as well?

In general, create two files via the 'touch' command, having timestamps of the seach limits.
Then use the test operators 'nt' and 'ot' of the posix shell or the 'find' option -newer/! -newer to get the requested files.

The man pages of 'test' and 'find' will help you.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
rajasekar.m
Advisor

Re: Find and remove files between two dates

i want to find and removes files in /var/cron/messages in a directory between two dates
Peter Nikitka
Honored Contributor

Re: Find and remove files between two dates

Hi (and welcome to this forum!),

try this:

touch -t 0602100000 /tmp/begin
touch -t 0602122359 /tmp/end
cd /var/cron/messages
for i in *
do
[ -f $i ] || continue
[ $i -nt /tmp/begin -a $i -ot /tmp/end ] && print rm $i
done

Remove the 'print' after your test to drop the file really.

mfG Peter

PS: Since you are new to this forum, I want to put your attention to ist unique point system:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
James R. Ferguson
Acclaimed Contributor

Re: Find and remove files between two dates

Hi:

Peter's solution is an excellent one if you don't want to recursively descend within a directory. If, on the other hand, you do want to find all candicate files in a directory and its subdirectories, this is another approach:

# touch -t 0602100000 /tmp/begin
# touch -t 0602122359 /tmp/end
# cd /dir
# find . -xdev -type f -newer /tmp/begin -a ! -newer /tmp/end | xargs rm

The '-xdev' prevents crossing mountpoints. The '-f' tells 'find' to look only for files. THe '-newer' and its negation with the "!" bound the criteria for file timestamps (using the last modification or 'mtime'). Piping the output to 'xargs' causes multiple arguments to be supplied to 'rm' at a time, thus spawning as few processes as possible to do the work needed.

Regards!

...JRF...