What is the absolute path of cd ~
on Ubuntu?
Trying to find out where this dir
is located?
For example:
/home
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityWhat is the absolute path of cd ~
on Ubuntu?
Trying to find out where this dir
is located?
For example:
/home
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.
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
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.
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.
cd ~
enterpwd
to find out. – shiftas Mar 26 '20 at 10:22