Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am looking for a BASH example of locate and rm -rf

I am thinking I assign the locate results to an array, then loop that array and rm -rf

Is this what you would do?

Thank you!

Jeff

share|improve this question
    
Can you provide an example of what you're trying to do with this? Combining the unfiltered output of a program with rm -rf is very dangerous. I'd put together a find based answer that narrows the risk factor a bit, but I'd need more detail. –  Andrew B Mar 5 '13 at 18:02
add comment

2 Answers

up vote 0 down vote accepted

if you really wanted to use locate and rm you could use something like the following:

for i in `locate item`; do rm -r -f $i; done

Be very careful with this though, because of the matching that is done with locate. If you are too general in what you type, you can delete a lot more than you originally wanted to.

You can at least get a list of the results by changing the command to:

for i in `locate item`; do echo $i; done
share|improve this answer
    
maybe replace rm -f with rm -i –  rfelsburg Mar 5 '13 at 18:02
    
The $i in the rm command should be "$i" to make sure that it doesn't try to rm each word individually if you have a filename with a space in it. –  DerfK Mar 5 '13 at 19:24
add comment

Generally this is done with find and rm, something like:

find . -name "foo" -exec rm -rf {} \;

share|improve this answer
1  
Preferably with a path prefix on {}. This narrows the scope of the rm -Rf if it goes out of control due to user error. –  Andrew B Mar 5 '13 at 18:04
    
Since rm can delete multiple files at once you can change \; to + as -exec rm -rf {} + and rather than running rm on one file at a time, it will run rm once with all of the files. –  DerfK Mar 5 '13 at 19:26
1  
@DerfK Risks an overflow of input args. That said, my suggestion is incompatible with -execdir, which is the preferred way of doing things. Combining rm with any sort of automation is just nasty business. –  Andrew B Mar 6 '13 at 0:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.