1

This is fairly trivial and I'm just curious as to why mkdir ~root/.ssh is the same thing as mkdir /root/.ssh? I'm reviewing the following docker file and the creator uses mkdir ~root/.ssh to create the .ssh directory:

https://github.com/macropin/docker-sshd/blob/master/Dockerfile

Is there any advantage to one over the other? When I first read it I assumed that ~root would expand to /rootroot.

4 Answers 4

5

~USER is just a shorthand notation for the home directory of user USER. For a normal user, this would typically be /home/USER, but for root, it is typically /root.

As for your question whether one is preferable to the other: The only difference is that ~root gets expanded dynamically to root's home directory, whereas /root is an absolute path that does not undergo any expansion process. What you want depends on your particular use case. If you want your script to work on machines where root's home directory lies elsewhere than in /root, then use ~root. If you want to make sure that the absolute path /root is always used, use /root.

In practice, it should not make any difference in most cases, though I would personally feel safer using /root unless I have reason to expect that my script will be run on machines where root's home directory is not /root.

4
  • How do you know that root's home directory is /root ? You said "but for root, it is /root" followed by "...on machines where root's home directory lies elsewhere than in /root"
    – Jeff Schaller
    Commented May 12, 2016 at 15:30
  • Also, tilde expansion is not bash-specific; see pubs.opengroup.org/onlinepubs/9699919799/utilities/…
    – Jeff Schaller
    Commented May 12, 2016 at 15:33
  • AIX and Solaris 10 both default to /; Solaris 11 appears to have moved to /root. Linux FHS
    – Jeff Schaller
    Commented May 12, 2016 at 15:51
  • @JeffSchaller Yes, that was an unfortunate formulation. I meant typically. Thanks for the pointer, I improved my post accordingly. Commented May 12, 2016 at 20:18
1

The symbol "~" is a special shortcut character that can be used to refer to your home directory.

~root will equal to the root users home directory, which is /root.
~ represents the logged users home directory, and if you use ~user it will return to you the user's home directory.

1

There's a distinct advantage to mkdir ~root/.ssh over mkdir/root/.ssh -- the former is flexible in its expansion of ~root to your root user's actual home directory. What if you decided that you wanted your root user's home directory to be /root2 or /home/root or anything besides /root?

0

As already mentioned in the other answers, it depends on where you intend to create the new directory. The form "mkdir /root/.ssh" unambiguously creates it at exactly that absolute path (assuming directory /root already exists and no other error). However, since you intend to create the directory .ssh which is typically used to store information required by ssh, my take would be that you should prefer the other form "mkdir ~root/.ssh" because it (assuming no other error) creates the new directory .ssh exactly where its expected by the ssh command - in the root user's home directory, irrespective of where exactly it's located in the filesystem, and which need not be /root always, though it typically is.

You must log in to answer this question.

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