. & .. & ~
. & ..
You may have noticed a couple of oddities when running ls -la
. Specifically, you may have noticed that .
and ..
are listed in the output.
.
represents the directory you are currently in. For example, if you are in the /depot/datamine/data
directory, and run ls -la
, you will see the following.
total 57 drwxrwsr-x+ 42 root tdm-admin 4096 Aug 17 13:08 . drwxr-s---+ 9 root tdm 4096 Aug 15 22:36 .. drwxrwsr-x+ 4 kamstut tdm-admin 4096 Sep 1 2019 8451 drwxrwsr-x+ 32 kamstut tdm-admin 4096 Jun 30 2020 airbnb drwxrwsr-x+ 2 kamstut tdm-admin 4096 Jun 22 2020 amazon ... drwxrwsr-x+ 2 kamstut tdm-admin 4096 Jul 28 2020 zillow
The .
in this context means the directory /depot/datamine/data
. If you run the following bash command, the .
is redundant and refers to the /depot/datamine/data
directory.
ls -la /depot/datamine/data/.
The ..
represents the parent directory, relative to the rest of the path. The parent directory is the directory that contains the directory you are currently in. For example, if you are in /depot/datamine/data/yelp
, the parent directory is /depot/datamine/data
.
..
is extremely useful. You can use it with any command line utility, for example, ls
.
# this will list the contents of the parent directory
ls -la ..
Any path that contains either .
or ..
is considered a relative path. Any path that contains the path, in its entirety, starting from the root directory, /
, is considered an absolute path.
For example, if we are currently in the /depot/datamine/data/yelp
directory, a relative path to /depot/datamine/data
is simply ..
, whereas the absolute path is /depot/datamine/data
.
~
~
represents the location which is in the $HOME
environment variable. If you change $HOME
, ~
will also change.
~
can be used just like .
or ..
— in the following example, we list 3 ways for moving to your home directory.
cd ~
cd $HOME
cd
In addition to representing the current user’s $HOME
directory, ~
can be combined with another’s username to represent that user’s $HOME
directory. Say that my username is kamstut
, but I want to reference a path in Dr. Ward’s $HOME
directory using his username, mdw
. I can use ~mdw
to represent his $HOME
directory, /home/mdw
.
echo ~ # /home/kamstut
echo ~kamstut # /home/kamstut
echo ~mdw # /home/mdw
echo ~mdw/projects # /home/mdw/projects
As you are navigating directories, to jump to the most previously visited directory, you can run ~-
. For example, if you navigate to /home/$USER/projects/project1/output
, then to /home/$USER
, and you’d like to jump directly back to /home/$USER/projects/project1/output
, you can run ~-
; the command is simply a reference to what is stored in the environment variable $OLDPWD
.
pwd # /home/kamstut
cd /home/kamstut/projects/project1/output
pwd # /home/kamstut/projects/project1/output
cd ~
pwd # /home/kamstut
cd ~-
pwd # /home/kamstut/projects/project1/output
Examples
If you are in the directory /home/kamstut/projects
, what is the relative path to /home/mdw/
?
Click to see solution
../../mdw