Remember_me
Remember_me

Reputation: 283

how to delete a file with quote in file name

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.

Upvotes: 10

Views: 11386

Answers (7)

Jonathan Komar
Jonathan Komar

Reputation: 3096

If you have a particularly stubborn file that has all kinds of characters

For example

''$'\336\017\200\336\024\b''J'$'\336\017\220\336\024\b''V'$'\336\017\240\336\024\b''^'$'\336\017\260\336\024\b''f'$'\336\017\300\336\024\b''p'$'\336\017\320\336\024\b''y'$'\336\017\340\336\024\b\202\336\017\360\336\024\b\212\336\017\337\024\b\223\336\017\020\337\024\b\234\336\017'

If it were me, I'd forget dealing with interpolation of strings (unless you are really good at using special characters) and use the file index number, or inode.

For example, assume your culprit is in the current working directory, then the following procedure would work:

# visually inspect list of files with index numbers (inodes) for the culprit
ls -i .  
# remove the culprit by inode
find . -maxdepth 1 -inum <inode> -exec rm {} \;

and be done with it.

Upvotes: 8

Jens
Jens

Reputation: 72619

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.

Upvotes: 15

user1309326
user1309326

Reputation: 36

Escape the quote with single quotes

$ touch '"  and spaces also "'
$ ls
"  and spaces also "
$ rm '"  and spaces also "'
$ ls
$

In your case:

$ rm 'data.log".2015-01-22"' 'data.log".2015-01-23"'

Upvotes: 2

Dale_Reagan
Dale_Reagan

Reputation: 2061

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:

ls | escape quotes | xargs rm  ## becomes something like:  
ls | tr "[\",']" '?' | xargs rm ## does not work on my system but this does:
rm -v $(ls *sing* *doub* | tr "[\",']" '?')

Output is:

removed `"""double"""'
removed `\'\'\'single\'\'\''

Now:

$ touch "'''single'''" '"""double"""'
$ ls -l *sing* *doub*
-rw-rw-r-- 1 dale dale 0 Feb 15 09:48 """double"""
-rw-rw-r-- 1 dale dale 0 Feb 15 09:48 '''single'''

If your patterns are consistent the other way might be to simplify:

$ rm -v *sing* *doub*
removed `\'\'\'single\'\'\''
removed `"""double"""'

For your example:

rm -v data.*${YEAR}-${MONTH}-${DAY}*  ## data.log".2015-01-22"  OR
rm -v data.*${YEAR}-${MONTH}-${DAY}?  ## data.log".2015-01-22"

Upvotes: 1

Barmar
Barmar

Reputation: 780663

ls | rm

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.

ls | xargs -d '\n' rm

But to just delete the files you want, quote the part of the name containing the single quote:

rm 'data.log"'.*

Upvotes: 1

choroba
choroba

Reputation: 241748

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.

Upvotes: 5

Avinash Raj
Avinash Raj

Reputation: 174696

You could do like this.

find . -type f -name '*"*' -exec rm {} +

Upvotes: 4

Related Questions