1

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.

Justin
  • 5,328
  • 19
  • 64
  • 84

2 Answers2

3
rm -- *-20*

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

ramruma
  • 2,740
  • 1
  • 15
  • 8
1

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*.

Andy Lester
  • 740
  • 5
  • 16