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

I'm familiar with how I would tackle this with PHP, however I'd like to get some more practice with bash scripting.

The task is to delete all files in a folder, which itself contains subfolders (with files). The files would typically be .pdf (or some variant: PDF, Pdf, pDf, ect), however there may occasionally be other file types including extensions unknown to me at this time.

Here's what I have so far. It echoes the filename, but if I issue rm $i, the system returns file not found on each file.

for i in `ls -bRC1 /foo/temp_folders/* ` ; do echo $i ; rm $i ; done

How would I force the absolute path when issuing rm $i?

share|improve this question
2  
Is there some reason why you wouldn't just use find? –  Zoredache May 9 '12 at 19:31

2 Answers 2

up vote 5 down vote accepted

Per Zoredache... why not:
find /foo/temp_folders/ -type f -iname * -exec rm {} +

Edit: changed the trailing \; to + for performance as noted here

share|improve this answer
    
I'd uprate you, but I don't have the mojo just yet - thanks for the response. –  a coder May 9 '12 at 20:16
3  
I wouldnt go with -exec rm {} + but rather simply with -delete -> it's much faster than constantly calling Linux's exec. The other day I had to delete tons of files and tried both, -delete option came out as absolute winner –  gyre May 9 '12 at 20:50
    
Nifty, I didn't realize find even had a -delete option... I'll have to remember that one. –  gharper May 10 '12 at 16:35

find -exec would be slow on large number of files. I would suggest:

find /foo/temp_folders -type f -print0 | xargs -0 rm
share|improve this answer
    
I'd uprate you, but I don't have the mojo just yet - thanks for the response. –  a coder May 9 '12 at 20:12
    
Actually, this is a good point, and not one I usually have to consider. I found an interesting article that compares "-exec {} \;" to "-exec {} +" to "| xargs" here: fahdshariff.blogspot.com/2009/05/find-exec-vs-xargs.html –  gharper May 9 '12 at 20:45

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.