-1

What is the absolute path of cd ~ on Ubuntu?

Trying to find out where this dir is located?

For example:

/home
2
  • 3
    after entering cd ~ enter pwd to find out. – shiftas Mar 26 '20 at 10:22
  • @shiftas thanks that works – sputn1k Mar 26 '20 at 10:26
2

Generally on linux it is /home/<your-username>/. You can see the path of the current directory if you enter pwd while you are in it.

1
  • thank you 'pwd' is exactly what I needed cheers - will accept your answer. – sputn1k Mar 26 '20 at 10:25
7

cd ~ takes you to your home directory. By default, the home directory is determined by the information stored in the “passwd” database (typically /etc/passwd on basic desktop systems, but central databases are often used in enterprise setups). However in a shell, cd ~ (and cd with no argument) looks at the value of the HOME variable, so a user’s home directory can be overridden (as far as the shell is concerned) by changing that variable. The HOME variable is initialised to point to the user’s home directory. If HOME is unset, cd ~ will revert to the default home directory (and cd will fail, at least in bash).

The canonical way to determine what a given user’s home directory is, before login, is to use getent:

getent passwd user

will show user’s entry in the database, and the home directory is the sixth field:

getent passwd user | cut -d: -f6
0
5

Usually, the HOME environment variable will tell you what the home directory is set to, for whichever user is logged in. I.e.:

echo $HOME

However, if the HOME variable is not set (from info bash):

If 'HOME' is unset, the home
directory of the user executing the shell is substituted instead.

You can find more information about Tilde Expansion in the info bash manual.

0
1

In quite all shells (e.g. *sh, *csh, *ksh), ~ is a shortcut for the current user home directory.

Many shells use HOME environment variable to "cache" the position of the home directory. The initial value of HOME is read from /etc/passwd file.

As @Time4Tea stated, the best way to check the current position of ~ is to print the HOME variable. E.g. echo $HOME.

Remember that the value of this variable is user dependant and that the user can change it if he needs.

Last but not least, shells usually support also the ~username shortcuts. E.g. ~andcoz is my (andcoz) home and not the current user's one.

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.