Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
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
5  
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. –  Pieter Geerkens Jul 30 '13 at 0:48
1  
More importantly, how do you delete a file named "/" ? –  Curt Jul 30 '13 at 1:44
2  
I always liked the file named * myself... –  RBerteig Jul 31 '13 at 5: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

3 Answers 3

up vote 44 down vote accepted

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
share|improve this answer
2  
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

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.

share|improve this answer
3  
Or, single it was a single char. rm -i ./? –  Hennes Jul 30 '13 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. –  Zack Jul 30 '13 at 13:31
4  
+1 for prefixing your glob with ./ –  evilsoup Jul 30 '13 at 17:08

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 $ 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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