11

Recently frigged up my external hard drive with my photos on it (most are on DVD anyway, but..) by some partition friggery.

Fortunately I was able to put things back together with PhotoRec another Unix partition utility and PDisk.

PhotoRec returned over one thousand folders chalk full of anything from .txt files to important .NEF's.

So I tried to make the sorting easier by using unix since the OSX Finder would simply crumble under such requests as to select and delete a billion .txt files.

But I encounter some BS when I tried to find and delete txt files, or find and move all jpegs recursively into a new folder called jpegs. I am a unix noob so I need some assistance please.

Here is what I did in bash. (I am in the directory that ls would list all the folders and files I need to act upon).

find . -name *.txt | rm

or

sudo find . -name *.txt | rm -f

So it's giving me some BS that I need to unlink the files. Whatever.

I need to find all .txt files recursively and delete them preferably verbose.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Propagator
  • 113
  • 1
  • 1
  • 4
  • add the -R option to your unix command. `find . -name *.txt | rm -Rf` http://www.computerhope.com/unix/urm.htm – Sense Sep 26 '12 at 14:53

3 Answers3

19

You can't pipe filenames to rm. You need to use xargs instead. Also, remember to quote the file pattern ".txt" or the shell will expand it.

find . -name "*.txt" | xargs rm
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • That seems to have taken care of the .txt files. For some reason when I did it before there where some txt files left. Maybe I ^C'd to early when my bash title bar was blipping rm and mv back and forth. Anyway that took care of it thanks. I'll make sure to quote the file pattern (it wasn't in my Lynda OSX Unix tutorial). – Propagator Sep 26 '12 at 15:20
  • 4
    That has the potential go gag on file names with spaces. If their find/xargs combo handles it, this is better: `find . -name "*.txt" -print0 | xargs -0 rm` to create ASCII NUL terminated strings. – Jeremy J Starcher Sep 26 '12 at 15:21
  • Yes indeed. Finder can find no .txt files. Awesome. – Propagator Sep 26 '12 at 15:23
16
find . -name "*.txt" -exec rm {} \;
mana
  • 6,347
  • 6
  • 50
  • 70
8
$ find  . -name "*.txt" -type f -delete
b3h3m0th
  • 1,330
  • 1
  • 11
  • 22