Operating System - HP-UX
1847552 Members
3088 Online
110265 Solutions
New Discussion

Re: Script for selected file deletion

 
SOLVED
Go to solution
PVR
Valued Contributor

Script for selected file deletion

I want to delete files with *.gz from /data directory and all it's sub directories

Can you help me with a script ?

Thanks in advance !
Don't give up. Try till success...
7 REPLIES 7
David DeWitt_2
Frequent Advisor

Re: Script for selected file deletion

Good morning,

I believe you can do this with one line.

find /data -name *.gz -exec rm {} \;

You could check before hand with...

find /data -name *.gz -exec ls {} \;

Hope this helps.

-dave
Pete Randall
Outstanding Contributor
Solution

Re: Script for selected file deletion

Or (to avoid spawning excess processes):

find /data -type f -name *.gz |xargs rm


Pete

Pete
Fred Ruffet
Honored Contributor

Re: Script for selected file deletion

Same as Pete, but just think to quote * char, to have it evaluated in each subdir :
find /data -type f -name "*.gz" | xargs rm

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
john korterman
Honored Contributor

Re: Script for selected file deletion

Hi,

I'd go for DeWitt's suggestion as I do not think that rm can read from std. in. Use a litte quoting in order not to expand files in the current directory, e.g.:
# find /data -name "*.gz" -exec rm {} \;
and you should of course first check by:
find /data -name "*.gz" -exec ls -l {} \;

regards,
John K.
it would be nice if you always got a second chance
David DeWitt_2
Frequent Advisor

Re: Script for selected file deletion

I just tested and the "xargs" will work on my system, but it causes problems if you are trying to be safe with a "rm -i".

-dave
Victor Fridyev
Honored Contributor

Re: Script for selected file deletion

Hi,

find /data -type f -name "*.gz" -exec rm {} \;
or
find /data -type f -name "*.gz"|xargs rm

Please pay your attention on double quotes in the command.

HTH
Entities are not to be multiplied beyond necessity - RTFM
PVR
Valued Contributor

Re: Script for selected file deletion

Thanks to all !
Don't give up. Try till success...