25

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
  • 1,001
  • 2
  • 12
  • 24
  • This requires the escape character, see [here](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_03.html) – MDMoore313 Jul 29 '13 at 18:28
  • possible duplicate of [Unix: Files starting with a dash, -](http://superuser.com/questions/120078/unix-files-starting-with-a-dash) – Ulrich Schwarz Jul 29 '13 at 18:28
  • 1
    Why do you have a file named that? (It doesn't even let me put it in a `code` block here in this comment!) – AJMansfield Jul 29 '13 at 19:59
  • 7
    More fun is trying to delete a file called , 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 Jul 30 '13 at 00:48
  • 1
    More importantly, how do you delete a file named "/" ? – Curt Jul 30 '13 at 01:44
  • 1
    @Curt `fsck`. Seriously. If a file named `/` exists, your filesystem is corrupt. – zwol Jul 30 '13 at 02:42
  • 2
    I always liked the file named `*` myself... – RBerteig Jul 31 '13 at 05:53
  • 1
    @Pieter Geerkens, +10, I shall try this! How do you create a file with the name of unprintable character? – Vorac Jul 31 '13 at 14:40
  • @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 Aug 23 '13 at 03:53

4 Answers4

52

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
Breakthrough
  • 34,227
  • 10
  • 105
  • 149
Hennes
  • 64,768
  • 7
  • 111
  • 168
  • 3
    Or, under the same principle, `rm '\'` (but *not* `rm "\"`). – evilsoup Jul 30 '13 at 17:06
  • 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 Jul 31 '13 at 17:50
16

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
  • 1,258
  • 8
  • 23
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
  • 151
  • 3
1

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.