2

A user (Bob)accidentally makes a subdirectory (bobsdir) in their Home directory and try to remove it but when they use the "rmdir \bobsfile" command they get "No Such Directory".

the Answer to the question is: rmdir ~Bob/\bobsdir

I get the "~" that is the same as /home but I cannot find in my study materials what the "\" are and why they work in this instance. I am a person that needs to know the "why" about stuff so any help would be greatly appreciated.

2
  • 3
    ` \ ` is an escape character. It doesn't realy make sense in this situation, but it's required when there's a space in the name, eg. bobs dir, then it would be rmdir ~/bobs\ dir. Also, there is no such thing as ~Bob/bobsdir, it's ~/bobsdir aka /home/Bob/bobsdir.
    – mreq
    May 10, 2013 at 18:15
  • yeah, looks like the questions is a "braindump" meaning everyone forgot all the small details when copying the question and answer, and there are many versions out there...
    – Mateo
    May 11, 2013 at 2:22

3 Answers 3

2

http://aiotestking.wordpress.com/category/linux/lpic-1/page/7/

This appears to be the exact question, without the extra slash in the answer.

However, when tested, it works either way... so, I looked around some more, and found yet another version picks the answer as double-backslashes:

http://studyhat.blogspot.com/2012/08/linux-question-answer.html

I would say the question may have slipped past the editors or reposters on various sites or books, but I'm interested to see if anyone else has an opinion as to what the extra backslashes after the home directory specification signify or are capable of.

There appears to be multiple ways to do this, but I'm not sure why... and one doesn't work at all (but seems indicated in many places as the 'correct' method - rmdir ~user/\\userdir).

2

I think you garbled the question, because as stated, it and the answer don't really match one another.

The backslash - \ - is used as an "escape" key in most Linux shells and many languages. What that means is that it either begins a special code, or nullifies a special character.

For example, the $ character has a special meaning, signifying a variable expansion, in the shell. But what if you want to name a file "$money"? You can't just touch $money because the shell will think you want to expand a variable named "money". So, you instead touch \$money, and now the shell will escape the $, treating it as a literal character instead of as a variable signifier.

root@banshee:/tmp# touch $money
touch: missing file operand
Try `touch --help' for more information.
root@banshee:/tmp# touch \$money
root@banshee:/tmp# ls -l | grep money
-rw-r--r-- 1 root    root       0 May 10 21:31 $money
root@banshee:/tmp# rm $money
rm: missing operand
Try `rm --help' for more information.
root@banshee:/tmp# rm \$money
root@banshee:/tmp# ls -l | grep money
root@banshee:/tmp# 

You can also use the escape character to "nerf" a space. Normally, if you have spaces in an argument, the shell will interpret it as separate keywords, rather than a single keyword with a space in it. But if you escape the space, the shell will interpret it as a single keyword with a space. Like this:

root@banshee:/tmp# touch bobs money
root@banshee:/tmp# ls | egrep '(bob|money)'
bobs
money
root@banshee:/tmp# rm bobs ; rm money
root@banshee:/tmp# touch bobs\ money
root@banshee:/tmp# ls | egrep '(bob|money)'
bobs money

The escape character also escapes itself. So, if you wanted to create a file called "\myfile", you'd need to do this:

root@banshee:/tmp# touch \\myfile
You have new mail in /var/mail/root
root@banshee:/tmp# ls -l | grep myfile
-rw-r--r-- 1 root    root       0 May 10 21:25 \myfile

And if you then want to remove \myfile, you would again escape the backslash with a second backslash, just like you did when you created it:

root@banshee:/tmp# rm \\myfile
root@banshee:/tmp# ls -l | grep myfile
root@banshee:/tmp# 

Alternately, you could wrap the filename in single quotes. Single quotes tell the shell that you don't want it to process spaces inside the quotes as meaning separate keywords, AND that you don't want variable or wildcard expansions to take place within the quotes:

root@banshee:/tmp# ls -l | grep myfile
-rw-r--r-- 1 root    root       0 May 10 21:28 \myfile
root@banshee:/tmp# rm '\myfile'
root@banshee:/tmp# ls -l | grep myfile

Note the implication here: if you actually want to make a filename that includes single quotes wrapped around it, you'll need to use the escape character to escape the single quotes!

root@banshee:/tmp# touch 'myfile'
root@banshee:/tmp# ls -l | grep myfile
-rw-r--r-- 1 root    root       0 May 10 21:29 myfile
root@banshee:/tmp# rm myfile
root@banshee:/tmp# touch \'myfile\'
root@banshee:/tmp# ls -l | grep myfile
-rw-r--r-- 1 root    root       0 May 10 21:29 'myfile'

Hope that helps make this clearer.

2
  • Ah so he made a directory named "/bobsdir"? or a file in "bobsdir" named "/bobsfile"
    – Mateo
    May 11, 2013 at 1:36
  • Honestly I'm not entirely sure what he did, from the way he phrased his question, but it did at least seem pretty clear that he needed help with the concept of an escape character. So, I gave lots of examples, hopefully some or all of which will help. :)
    – Jim Salter
    May 11, 2013 at 1:42
0

Explaination on why to use ~Bob/

Usually on your own Linux computer you use ~/ for your Home directory. The reason for this is that we usually assume you are logged in as the user you are using, and typing ~/ will take you to the home of the user you are logged in as.

So some situations using ~/ or just ~ will not work.

  1. Logged in as root
  2. Logged in as another user

In this case we can use ~Bob specifying the home folder of user "Bob"

This explains why to use that form of home folder

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .