Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I delete all files containing -20 in the filename in the current directory?

I have

ls | grep "-20" | rm -rf

But that does not work. Ideas?


share|improve this question
up vote 3 down vote accepted
rm -- *-20*

The -- stops -20 being treated as an argument to rm if the file -20 exists.

share|improve this answer

Your problem with

ls | grep "-20" | rm -rf  # DOESN'T WORK

is that you're piping filenames to rm but rm doesn't read from standard input. You need to use xargs to call rm repeatedly like:

ls | grep '-20' | xargs rm -rf

But really, you don't want to use ls | grep to find file names. Use the find command.

find . -name '*-20' | xargs rm -rf

But of course as the other poster said, the best solution is to rm -- *-20*.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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