Take the 2-minute tour ×

I found several examples that work as long as there are no spaces in the filenames. Unfortunately, I have spaces in my filenames (not my choice, in fact they are the ones I need to remove).

So, for example, this is what I thought would work:

find . -type f | grep ).MOV | xargs rm

because I want to delete the files like:

  • L112 (1).MOV
  • L113 (1).MOV
  • L113 (2).MOV
  • etc.

and I want to keep the files like

  • L112.MOV
  • L113.MOV
  • L114.MOV
  • etc.

This finds the files correctly:

find . -type f | grep ).MOV

but when I pipe it to rm, the spaces in the filename screw it up.

Any help would be greatly appreciated.

share|improve this question

migrated from stackoverflow.com Aug 27 '09 at 2:19

This question came from our site for professional and enthusiast programmers.

add comment

5 Answers

up vote 5 down vote accepted

rm *\).MOV
share|improve this answer
D'oh! Yeah, that's probably better than using find :-). –  Matt Solnit Aug 26 '09 at 19:24
The OP tried to make it confusing for us :) –  Kevin Lochner Aug 26 '09 at 19:25
Thank you, I was surprised that I couldn't find this answer with Google. So simple. –  Craig Nakamoto Aug 28 '09 at 16:07
add comment

Try this:

find . -name "*).MOV" -exec rm {} \;
share|improve this answer
add comment

In most Unix installations you can denote a space in a filename by prefixing it with a backslash. So for instance, try this out:

mkdir blah
cd blah
touch file1
touch file2
touch "file 3"
touch "file 4"

Do an "ls" and you should see this:

file 3 file 4 file1 file2

Now do "ls *\ *" and you should see everything with a space in it:

file 3 file 4

So maybe just an "rm *\ *" is all you need? Or modify that regular expression slightly to fit your needs.

Hope that helps!

share|improve this answer
add comment

Tell xargs to use newlines as delimiters:

find . -type f | grep ).MOV | xargs -d'\n' rm

Better, have find do the matching and use -print0 and xargs -0 to allow any character in the file names, even \n!

find . -type f -name '*).MOV' -print0 | xargs -0 rm

Even better, just have find call rm directly.

find . -type f -name '*).MOV' -exec rm {} +

Best, skip rm and have find do the deleting.

find . -type f -name '*).MOV' -delete

Or in this specific case, who needs all the heavy machinery?

rm *\).MOV
share|improve this answer
based on the way the question was asked this seems like the appropriate answer "how do I delete files that contain a string" -- should read "filename" –  qodeninja Feb 2 '12 at 21:25
add comment

Many unix tools are fragile (or at least tricky to use correctly) in the presence of file names containing spaces and tabs, or even worse, newlines, so when you have a choice, you should avoid these characters in file names.

However, if you want to remove all files in the current directory (and all subdirectories) with a space in the name, the following will work.

find . -name '* *' -print0 | xargs -0 rm -f

The -print0 option tells find to use the NUL byte (\0) as the separator between file names instead of NL (\n). Conversely, -0 tells xargs to use NUL as the line separator instead of NL. Alter the pattern argument to the -name option as needed to match the files you want to remove.

It can be a bit tricky to prevent find from recursing into subdirectories (although check out the -maxdepth argument), so if you only want to affect files in the current directory you might want to approach the problem differently.

Overall, I strongly recommend becoming fluent with find and xargs. Individually, and especially when chained together as shown above, they make an extremely flexible toolkit for bulk file operations.

share|improve this answer
add comment

Your Answer


