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:


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


~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.


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.


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?


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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