I am trying to remove all files in a Windows 7 directory with filenames containing brackets; ( and ):

Using MinGW:

rm *(*)*

I get an error:

sh: syntax error near unexpected token '('

I assume this is because the rm command sees a bracket as some sort of special input. What could I do instead?


You can "escape" specific metacharacters by prefixing with backslash.

$ touch 'aaa(bbb)ccc'
$ rm *\(*\)*
$ ls
  • rm '*(*)*' gives me rm: cannot lstat *()': No such file or directory. rm *(*)*` works (but also wants to delete directories with this match but that's what the original of the OP wants too, giving an error-line). – Rik Oct 8 '13 at 9:39
  • Using quotes did not work for me. The escape backslash did though. @Rik - Apologies - I was not specific. The directory I am looking at has no sub-directories. – atomh33ls Oct 8 '13 at 9:45
  • 1
    @RedGrittyBrick The first method only works on the one file named *(*)*, but OP wants to work on all files containing ( and ). Try it yourself with a touch 'a(b)c'. Then rm '*(*)*' does not work on all these files. Also not in bash on Linux! The rm *\(*\)* does work on both Linux and mingw. – Rik Oct 8 '13 at 9:57
  • 1
    @Rik: Thanks, I was aware of that (which is why I mentioned "all metacharacters") - but maybe it is confusing to include that in this answer - I'll remove it. – RedGrittyBrick Oct 8 '13 at 10:19

You can do the following:

find . -type f -name "*(*)*" -delete -maxdepth 1

For testing I would use the -print argument first:

find . -type f -name "*(*)*" -print -maxdepth 1

If you want to do it in all subdirectories you can ommit the -maxdepth 1

If the -delete does not work you can try:

find . -type f -name "*(*)*" -exec rm -rf {} \;

Edit: Included the -type f to only do this on files (and not directories) same as in rm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.