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

7,519

Solution 1

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

Solution 2

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.

Solution 3

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 $ 

Solution 4

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.

Share:
7,519

Related videos on Youtube

Nosrettap
Author by

Nosrettap

I recently graduated from Duke University as a computer science and economics double major. I am now working full time as a software developer. I am proficient in Objective-C and Java, and I know (to some degree) C, C++, Python, Perl, SML, Assembly, HTML, CSS, JavaScript.

Updated on September 18, 2022

Comments

  • Nosrettap
    Nosrettap over 1 year

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

  • ganesh
    ganesh over 10 years
    Or, single it was a single char. rm -i ./?
  • Miguel Tejada
    Miguel Tejada over 10 years
    Yeah. If you can write a more specific glob than ./*, that's always a good idea, especially when doing something destructive.
  • evilsoup
    evilsoup over 10 years
    Or, under the same principle, rm '\' (but not rm "\").
  • evilsoup
    evilsoup over 10 years
    +1 for prefixing your glob with ./
  • Breakthrough
    Breakthrough over 10 years
    @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).
  • D.A.H
    D.A.H over 2 years
    Best answer! Works!