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.
rm -- *-20*
The -- stops -20 being treated as an argument to rm if the file -20 exists.
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*
.