Operating System - HP-UX
1748127 Members
3639 Online
108758 Solutions
New Discussion юеВ

delete files by date range

 
SOLVED
Go to solution
ng_7
Regular Advisor

delete files by date range

hi, does anyone knows how to delete files by specifying the modify date range ? eg to delete file with modify date from 01/01/07 to 28/02/07 .

thanks
12 REPLIES 12
Steven Schweda
Honored Contributor

Re: delete files by date range

Yogeeraj_1
Honored Contributor
Solution

Re: delete files by date range

hi,

try this solution:
touch -t 0701010000 /tmp/start
touch -t 0702282359 /tmp/finish
find . -xdev -type f -newer /tmp/start -a ! -newer /tmp/finish | xargs rm

(this is based on one solution that JRF provided once)

hope this helps!
kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
ng_7
Regular Advisor

Re: delete files by date range

thanks , these are the command i wanted. it works fine. and how about moving to another directory instead of removing it .

thanks
Pete Randall
Outstanding Contributor

Re: delete files by date range

find . -xdev -type f -newer /tmp/start -a ! -newer /tmp/finish -exec mv {} /newdir \;


Pete

Pete
Dennis Handly
Acclaimed Contributor

Re: delete files by date range

>how about moving to another directory instead of removing it.

You may have to execute a script so you can change the path:
find . -xdev -type f -newer /tmp/start -a ! -newer /tmp/finish \
-exec move_them_script {} +

And the contents of move_them_script:

#!/usr/bin/ksh
for file in "$@"; do
echo mv $file new_path/
done

Make sure it does what you want then you can remove "echo".

If find returns a "small" amount of files you can do:
$ mv $(find . -xdev -type f -newer /tmp/start -a ! -newer /tmp/finish) new_path
Peter Nikitka
Honored Contributor

Re: delete files by date range

Hi,

note, that Pete's and Dennis' wont preserve the directory structure the file was found but move it plain into the new directory.
So it may be, that filenames in different directories overwrite eachother.

If this is a problem, you'll have to modify Dennis' script like this:
#!/usr/bin/ksh
for file; do
np=new_path/${file%/*}
[ -d $np ] || mkdir -p $np
echo mv $file $np
done

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"
ng_7
Regular Advisor

Re: delete files by date range

hi, thanks, sorry, one more question :

to move a specific file with a specific range of modified date to other directory.
eg : to move *.t with the modified date range from 01/01/07 to 28/02/07 to /newdir

thank you very much
A. Clay Stephenson
Acclaimed Contributor

Re: delete files by date range

simply add one more condition to your find:
-name '*.t'
If it ain't broke, I can fix that.
ng_7
Regular Advisor

Re: delete files by date range

if i am using the below command :

find . -xdev -type f -newer /tmp/start -a ! -newer /tmp/finish -exec mv {} /newdir \;

then i add -name *.t here on the find command (without moving)

find . -name *.t -xdev -type f -newer /tmp/start -a ! -newer /tmp/finish \;
result : find: missing conjunction

then i try this
find . \( -name *.t \) -xdev -type f -newer /tmp/start -a ! -newer /tmp/finish \;

result :
find: bad option OFAAAa00102.t

please help. thanks