Recursive rm w/ wildcard?

jcooper11

Registered
Want to do recursive rm w/ a wildcard, deleting all files named, for example, *.jpg, but leaving others, going all the way down inside a folder/directory.
Tried variations on ....
rm -r *.jpg
Not allowed? Another approach?
tia
 
find . -name "*.jpg" | xargs rm

YMMV and it will break for file with spaces in their names, the space case is left as an exercise for the reader.
 
That will not get /foo/bar.jpg

The thing to remember is that the shell does the * expansion (globbing) and not rm. So you are always saying recursively remove these directories. Not recurse through these directories looking for these files.
 
*BUZZZT*

Thank you all for playing, please enjoy your parting gifts.

(Those command you type, they are not doing what you think they are doing...)
 
lurk said:
*BUZZZT*

Thank you all for playing, please enjoy your parting gifts.

(Those command you type, they are not doing what you think they are doing...)

rm -rf [filename(s)]

will remove the given files in the current or given directory, recursively. After about 7 years experience with Linux I think I know this command pretty well.

Your post tells me that you do not seem to have much experience with Linux.
 
lurk said:
That will not get /foo/bar.jpg

The thing to remember is that the shell does the * expansion (globbing) and not rm. So you are always saying recursively remove these directories. Not recurse through these directories looking for these files.

Yeah, woops. :(
 
HomunQlus said:
rm -rf [filename(s)]

will remove the given files in the current or given directory, recursively. After about 7 years experience with Linux I think I know this command pretty well.

Your post tells me you either don't know much about Linux or that reading is not your strong point. The original question was:

Want to do recursive rm w/ a wildcard, deleting all files named, for example, *.jpg, but leaving others, going all the way down inside a folder/directory.

The recursive thing that rm does is remove directories. It does not traverse them looking for matches to wild cards. That is why my suggested solution made use of find, the tool for discriminating recursive directory traversals. Do you see your error?

HomunQlus said:
Your post tells me that you do not seem to have much experience with Linux.

Just because we are on a Mac board does not mean that my credentials aren't better than yours. Just for the pissing match I have been using Linux since '94, Solaris since '96 and I used to work in HP's Unix Systems Lab.

Humm.... You could always assume I don't have a clue though if it makes you happy.
 
lurk said:
The recursive thing that rm does is remove directories. It does not traverse them looking for matches to wild cards. That is why my suggested solution made use of find, the tool for discriminating recursive directory traversals. Do you see your error?
.

Speaking of errors: you said to use find with xargs.

As OS X is mainly GUI, it is quite common to get files that have spaces on their
names. xargs takes lines from its input, and calls its command with enough
(I guess it is told in compilation time) parameters. So if you have files

a.jpg
b.jpg
c d.jpg

xargs will call

rm a.jpg b.jpg c d.jpg

and you get error message about files c and d.jpg.

Instead, give to both find and xargs parameter -0. Find will append ascii
character number 0 (which is normally used; in Unix it and / are only illegal
characters), and xargs notices it, so it will call

rm "a.jpg" ".jpg" "c d.jpg" (or its efect).

And before anyone tells to use find with -exec, as I told, xargs calls the
command with several filenames at once, so it is faster.
 
artov said:
Speaking of errors: you said to use find with xargs.
Well, If you go all the was back to my original reply I did mention that it would not work for spaces and that that was left as an exercise for the reader. You are right that -0 is the way to go but I assumed that they would figure that out.
 
HomunQlus said:
rm -rf [filename(s)]

will remove the given files in the current or given directory, recursively. After about 7 years experience with Linux I think I know this command pretty well.

Your post tells me that you do not seem to have much experience with Linux.

That will only recursively remove *.jpg files recursively, provided there were *.jpg files in the current directory to begin with. If you're starting from /, or any directory where there isn't a *.jpg file, it won't work. You'll need to do what lurk posted.
 
Back
Top