May 9, 2012
tom

Recursive deletion of files (bash)

Question

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?

Asked by a coder

Answer

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

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

Answered by gharper

Related posts:

  1. How do I rename multiple files by removing characters in bash?
  2. Delete recursive directorys with FTP command on Bash
  3. grep/list bash recursive
  4. Delete recursive directories with FTP command on Bash
  5. View files set for deletion during the next system restart

Leave a comment