Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

Thanks.

share|improve this question

2 Answers 2

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

 
discard

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.