10

I have a pesky little file whose name appears to be single character, and that character is a \r

How can I delete this?

This is what I get with ls -bl:

-rw-rwxr--+   1 root             snapplewriters        0 Aug 29  2011 \r

ls -l just display it "?"

1
  • How did you even end up with a file named like that?
    – mkaito
    Apr 4, 2012 at 1:57

5 Answers 5

16

Us ls -li to get the inode number for the file (first column), then use find to delete it (assuming inode is 12345):

find . -inum 12345 -exec rm -i {} \;

3
  • Thank you - this is extremely helpful - I felt like there must be some kind of unequivocal system reference to the file but couldn't find it (quickly anyway).
    – Colin
    Apr 3, 2012 at 16:44
  • 1
    Depending on the version of find installed, you could also do find . -inum 12345 -delete Apr 3, 2012 at 21:01
  • For me, the more version-agnostic it is, the better. But good suggestion none-the-less. :)
    – Garrett
    Apr 3, 2012 at 21:10
14

use ANSI-C quoting: rm $'\r'

http://www.gnu.org/software/bash/manual/bashref.html#ANSI_002dC-Quoting

0
5

I would personally reach for Python:

>>> import os
>>> '\r' in os.listdir('.')
True
>>> os.unlink('\r')

But you can also do this from the shell if you understand escape characters.

$ ls -b $'\r'
\r
$ rm -vi $'\r'
rm: remove regular empty file ‘\r’? y
removed ‘\r’
1
  • I guess you also have a sledgehammer in your kitchen? Just in case you need to crack a nut? :-)
    – mivk
    Nov 27, 2015 at 18:18
3

Another quick way to remove a file called <carriage return> if you're using the Bash shell is:

$ rm <control-v><control-m>
2

Single-character file names are unusual, and if you don't have any other such files in your directory, you can try this:

rm ?

I'm too lazy to learn or look up how to quote strange characters, so I've used variants of this a number of times when I was stuck with files with difficult names.

2
  • -1 This matches all files with a single character name - See "Pathname Expansion" in man bash. Try touch a b c d $'\r'; rm ?
    – l0b0
    May 15, 2012 at 14:20
  • 1
    @l0b0: Yes? As I wrote, single-character file names are unusual. It is fairly likely that there are no other such files in the directory, and in that case, this works. Otherwise "rm -i ?" can be used. May 15, 2012 at 20:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .