How can I remove a file or directory called "\"?

20

2

I'm using terminal on a Ubuntu machine and there is a file that I would like to delete. The file's name is \ (just a backslash).

Now usually I would just do

rm filename

However if I do rm \ then it thinks I'm trying to write a multi-line command.

How can I delete this file? I know that I could just use the GUI file system, but that's not very efficient.

So, how can I delete (in terminal) a file called \?

Nosrettap

Posted 2013-07-29T18:24:14.443

Reputation: 711

This requires the escape character, see here

– MDMoore313 – 2013-07-29T18:28:06.387

possible duplicate of Unix: Files starting with a dash, -

– Ulrich Schwarz – 2013-07-29T18:28:21.497

1Why do you have a file named that? (It doesn't even let me put it in a code block here in this comment!) – AJMansfield – 2013-07-29T19:59:20.747

7More fun is trying to delete a file called <BEL>, aka Ctrl-G if I recall correctly. Everytime you do a ls the keyboard beeps at you until you (a) discovered the invisible file; and (b) determined how to delete a file with only one unprintable character in its name. – Pieter Geerkens – 2013-07-30T00:48:23.610

1More importantly, how do you delete a file named "/" ? – Curt – 2013-07-30T01:44:53.650

1@Curt fsck. Seriously. If a file named / exists, your filesystem is corrupt. – zwol – 2013-07-30T02:42:23.323

2I always liked the file named * myself... – RBerteig – 2013-07-31T05:53:22.133

1@Pieter Geerkens, +10, I shall try this! How do you create a file with the name of unprintable character? – Vorac – 2013-07-31T14:40:20.873

@Vorac: Now that you know it is possible - figure it out yourself. It's the knowing it's possible that is the real challenge; the rest is just good clean fun. – Pieter Geerkens – 2013-08-23T03:53:19.760

Answers

48

Use rm \\ (escape the backslash with another backslash). Note that this also works similarily, for directories named \ (using either rmdir, or rm with the -r flag).

Example:

>mkdir demo
>cd demo
>touch \\
>ls -l
total 0
-rw-------  1 hennes  users  0 Jul 29 20:25 \
>rm \\
>ls -l
total 0

Hennes

Posted 2013-07-29T18:24:14.443

Reputation: 60 739

3Or, under the same principle, rm '\' (but not rm "\"). – evilsoup – 2013-07-30T17:06:36.273

2@evilsoup just to provide some additional clarification on that, the \ character is used as an escape character in double-quote delimited strings. Saying rm "\" will be parsed into an unclosed string, as the second quotation mark is used with an escape character (and thus will be parsed as the double-quote character itself, and not the end of a string). Thus, the terminal will wait until you finish the string with another ". The equivalent method to use double quotes here would be rm "\\" (which is directly equivalent to both rm '\' and rm \\, as you already confirmed). – Breakthrough – 2013-07-31T17:50:33.440

15

A general tactic for manually deleting files with awkward characters in their names is

rm -i ./*

This will prompt you to choose whether or not to delete each file in the directory.

zwol

Posted 2013-07-29T18:24:14.443

Reputation: 1 130

3Or, single it was a single char. rm -i ./? – Hennes – 2013-07-30T11:52:01.307

1Yeah. If you can write a more specific glob than ./*, that's always a good idea, especially when doing something destructive. – zwol – 2013-07-30T13:31:31.010

4+1 for prefixing your glob with ./ – evilsoup – 2013-07-30T17:08:05.903

12

You can also unlink by referencing the inode of a file

linus ~/test $ touch \\
linus ~/test $ ls -li
total 0
15204561 -rw-r--r-- 1 pat sudo 0 Jul 29 23:03 \
linus ~/test $ find . -inum 15204561 -exec rm -v {} \;
removed `./\\'
linus ~/test $ ls -li
total 0
linus ~/test $ 

joshbaptiste

Posted 2013-07-29T18:24:14.443

Reputation: 141

0

Please check inode of file first . ls -li

137791 -rw-rw-r--. 1 svr svr 366 Mar 11 15:57

inode of "\" is "137791 and then use find command to delete "\" with inode number.

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

rm: remove regular file `./\'? yes

"\" will be removed then.

BALVINDER SINGH

Posted 2013-07-29T18:24:14.443

Reputation: 1