24

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 \?

9
  • 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!) Jul 29, 2013 at 19:59
  • 7
    More 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. Jul 30, 2013 at 0:48
  • 1
    More importantly, how do you delete a file named "/" ?
    – Curt
    Jul 30, 2013 at 1:44
  • 1
    @Curt fsck. Seriously. If a file named / exists, your filesystem is corrupt.
    – zwol
    Jul 30, 2013 at 2:42
  • 2
    I always liked the file named * myself...
    – RBerteig
    Jul 31, 2013 at 5:53

4 Answers 4

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
2
  • 3
    Or, under the same principle, rm '\' (but not rm "\").
    – evilsoup
    Jul 30, 2013 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). Jul 31, 2013 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.

3
  • 3
    Or, single it was a single char. rm -i ./?
    – Hennes
    Jul 30, 2013 at 11:52
  • 1
    Yeah. If you can write a more specific glob than ./*, that's always a good idea, especially when doing something destructive.
    – zwol
    Jul 30, 2013 at 13:31
  • 4
    +1 for prefixing your glob with ./
    – evilsoup
    Jul 30, 2013 at 17:08
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 $ 
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.

1
  • Best answer! Works!
    – D.A.H
    Sep 1, 2021 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.