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,041

4 Answers4

51

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,847
Hennes
  • 65,804
  • 7
  • 115
  • 169
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,268
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.