When I do ls
in my directory, get bunch of these :
data.log".2015-01-22"
data.log".2015-01-23"
However when I do this :
rm: cannot remove `data.log.2015-01-22': No such file or directory
If I could somehow do something line ls | escape quotes | xargs rm
So yeah, how do I remove these files containing "
?
Update
While most answer work. I was actually trying to do this :
ls | rm
So it was failing for some files. How can I escape quote in pipe after ls
. Most of the answers actually addresses the manual manipulation of file which works. But I was asking about the escaping/replacing quotes after the ls
. Sorry if my question was confusing
Use single quotes to quote the double quotes, or backslash:
Otherwise, double quotes are interpreted by the shell and removed from the string.
Answer to the updated question:
Don't process the output of
ls
. Filenames can contain spaces, newlines, etc.1st - suggestion - modify the tool creating file names with quotes in them... :)
Try a little wild-char magic - using your tool of choice, i.e I would use
tr
:Output is:
Now:
If your patterns are consistent the other way might be to simplify:
For your example:
doesn't work because
rm
gets the files to remove from the command line arguments, not from standard input. You can use xargs to translate standard input to arguments.But to just delete the files you want, quote the part of the name containing the single quote:
If you only need to do this once in a while interactively, use
and answer y or n as appropriate. This can be used to get rid of many files having funny characters in their name.
You could do like this.
Escape the quote with single quotes
In your case: