Operating System - HP-UX
1833030 Members
2231 Online
110049 Solutions
New Discussion

Simple script to delete files

 
SOLVED
Go to solution
Laurie_2
Advisor

Simple script to delete files

Hi all you script guru's:

I need a script to go thur
all the files to find
Makefile.lcl and delete that file (but not delete Makefile).

I did a:

find / -name Makefile.lcl

and got 7 pages and I was
deleting them one a time and
I said hey a script could do
this.

I know it's very simple but
I can't afford to delete anything else (unemployment
seems scarey in today's
economy).

Could someone send me a
simple and safe delete script?
I'm using HP-UX 11.0 csh but
I'm open to any shell type.

TIA,
Laurie
How can you make the world a better place
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor

Re: Simple script to delete files

How about this:

find / -name 'Makefile.lcl' -exec rm {} \;

I really prefer not to do a find from / but rather to cd to the desired directory and do a find . instead. Before you do the -exec rm {} \; I suggest that you find do a harmless command like -exec echo {} \; first.

If it ain't broke, I can fix that.
harry d brown jr
Honored Contributor
Solution

Re: Simple script to delete files

laurie,

just extend your "find"

find / -name "Makefile\.lcl" -exec rm {} \;

Note the quotes and "escaped" period to get an exact file match.

live free or die
harry
Live Free or Die
Rodney Hills
Honored Contributor

Re: Simple script to delete files

If you have a lot of these files, then a more efficient form of find is to pipe the file names to "xargs" to remove multiple files in fewer forked processes.

find / -name Makefile.lcl -print | xargs rm -f

-- Rod Hills
There be dragons...
Craig Rants
Honored Contributor

Re: Simple script to delete files

To do this in a script:
#!/bin/sh
find / -name 'Makefile.lcl' > /tmp/make.list
for i in `cat /tmp/make.list`
do
rm $i
done

The find should work faster, but for loops will always be your friend.

GL,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Alan Riggs
Honored Contributor

Re: Simple script to delete files

find from / is a performance killer. Use du -a instead if you care about system load:

du -a |awk '$2 ~ "Makefile.lcl$" {print $2}|xargs rm