On my macOS system, I have a folder with 7,000+ images, and need to delete the images that have a hyphen in the filename. I have tried the following commands without getting a response after I cd into the directory (I am new to terminal):

rm -rf -- -
$ rm ./-
find . -type f -regex '/-/' -exec rm {} \;
find . -name '-*' | xargs rm
find . -regex '/-/' -print0 | xargs -0 rm

1 Answer 1


The pattern *-* will match any name in the current directory that contains a dash (literally "a name that contains at least one -, possibly prefixed and/or suffixed by some other string"). This means that you should be able to do

rm ./*-*


rm -- *-*

to delete all files with names containing a - character in the current directory.

In the zsh shell, using *-*(.) as the pattern would restrict the match to only regular files (not directories, although rm would not be able to delete directories anyway).

If these are too many files for rm to handle in one go, use find like so:

find . -maxdepth 1 -type f -name '*-*' -delete

I'm using -maxdepth 1 to restrict the search to only the current directory, and I added -type f to only delete regular files.

You could also use a (somewhat slow) loop:

for name in ./*-*; do
    rm "$name"

Some people prefer "one-liners", and this loop could be written as

for name in ./*-*; do rm "$name"; done

... although typing it as a multi-line command would definitely be possible in most modern shells.

  • Thank you so much for this and for explaining each one. Really appreciate it. This is what worked - find . -maxdepth 1 -type f -name '-' -delete
    – tonjaggart
    Mar 12, 2020 at 23:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .