Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to delete a file with quote in file name

Tags:

bash

shell

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 a quote in a 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.

like image 840
Remember_me Avatar asked Dec 06 '22 22:12

Remember_me


2 Answers

If you only need to do this once in a while interactively, use

rm -i -- *

and answer y or n as appropriate. This can be used to get rid of many files having funny characters in their name.

It has the advantage of not needing to type/escape funny characters, blanks, etc, since the shell globbing with * does that for you. It is also as short as it gets, so easy to memorize.

like image 196
Jens Avatar answered Dec 30 '22 09:12

Jens


Use single quotes to quote the double quotes, or backslash:

rm data.log'"'*
rm data.log\"*

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.

like image 42
choroba Avatar answered Dec 30 '22 08:12

choroba