Everything in the File System (FS) and commands that affect it are based around relative and absolute paths. Refer to the Shell introduction articles for a more detailed explanation. Below is a practical refresher:
/
)You will notice that most of the FS related commands make use of path arguments that can be written as relative or absolute paths.
Let’s begin by reviewing the essential commands for navigating the FS from the command-line. If you have not already set up the Ubuntu VM with Bash refer to the installation walkthrough article before continuing.
Note
While you can simply read these commands and trust their outputs it is important that you try them on your own machine. The only way to integrate a new tool in your workflow is to practice with it!
Most commands will provide documentation through the --help
option or a man
(manual) entry. When you are unsure about a command or want to learn more about how it is used you can use get help like this:
$ command --help
# prints help documentation
$ man <command name>
# enters a documentation viewer
# scroll up with J key, down with K key
# quit with Q key
Note
Like the man
command you will find that many tools in Bash rely strictly on keyboard input rather than the use of a mouse. While this may seem foreign at first you will eventually get comfortable (and fast) at working without a mouse. Most of the CLI tools will follow the standard conventions for scrolling (J
and K
) and quitting (Q
).
The pwd
(print working directory) command will give you the absolute path of your current working directory (CWD) in the FS:
$ pwd
/home/student
If you are in a new Shell session it will default to a CWD of /home/<username>
.
The cd
(change directory) command takes one argument – the relative or absolute path of where you want to go:
# relative paths begin with a './'
$ cd ./path/name
# or just the path name with no leading '/'
$ cd path/name
# absolute paths always begin from the root (/) directory
cd /home/student/path/name
If you want to change to a directory using a relative path that is under your CWD this is straightforward. But what if you need to refer to a relative path above your CWD? For this Bash includes two special characters for relative references:
We will discuss the use of the this directory character (.
) soon. Consider the following example:
/home/student
/Downloads
/album
/Media <-- your target
/Videos <-- your CWD
If you want to move to the Media directory relative to Videos you need to go up one directory level:
$ pwd
/home/student/Media/Videos
$ cd ../
# for going up one directory only you can leave off the trailing '/'
$ cd ..
$ pwd
/home/student/Media
What if you again start inside Videos
and you want to switch to the album
directory?
/home/student
/Downloads
/album <-- your target
/Media
/Videos <-- your CWD
Relative to where you are, you need to:
Media
parent directory: ../
Media
and Downloads
are: ../../
Downloads
: ../Downloads
album
: ../Downloads/album
$ pwd
/home/student/Media/Videos
$ cd ../Downloads/album
$ pwd
/home/student/Downloads/album
This process can be repeated for going up (../
) or down (/
) as many times as needed to create the proper relative path. When in doubt check your CWD!
There are also two useful shorthands for quickly navigating around:
~
: the tilda (next to the 1
key) is a shorthand for the home directory of the logged in user (relies on the $HOME
environment variable)-
: the dash character (next to the 0
key) is a shorthand for returning to the previous CWD (thanks to the $OLDPWD
environment variable)$ pwd
/home/student/Media
$ cd ~
$ pwd
/home/student
$ cd -
$ pwd
/home/student/Media
The ~
shorthand can also be used as a base relative to HOME path:
$ pwd
/home/student/Media
$ cd ~/Downloads/album
$ pwd
/home/student/Downloads/album
Our final navigation command is ls
(list contents). As mentioned previously ls
can be used with no arguments to view the contents of the CWD:
$ pwd
/home/student
$ ls
# contents of CWD ("empty" for a new user)
But ls
can also be used view the contents of another directory using a relative or absolute path as its argument:
$ pwd
/home/student
# absolute path
$ ls /usr/bin
# contents of the user binaries directory
# relative path
$ ls ../../usr/bin
You can provide options to ls
to change the behavior of its output.
The -a
option means all and modifies ls
to show all the files, both visible and hidden files.
Tip
Hidden files are special configuration files that are hidden to prevent accidental changes to them from consumers. However, when working with CLI tools you will often use these dot files as a way of configuring the way your tools behave on your machine.
While the home directory appeared empty earlier it actually contained several hidden files:
$ pwd
/home/student
$ ls -a
# hidden files like .bashrc, .profile
The -l
option outputs in long form which shows additional details about the contents.
In the following example it is combined with -a
to see detailed information about the hidden files in the home directory:
$ pwd
/home/student
# or shorthand: ls -al
$ ls -a -l
In this output you can view details like the file type and access mode as well as the user and group that owns the file.
We will not go into permission modes and ownership in this class. However, it is worth knowing that regular files are denoted by a -
character and directory files by the d
character (on the far left of each file’s information).
Fun Fact
Notice how the .
and ..
are actually listed as directory files (the first d
in the long output).
The .
and ..
are actually treated as files (because everything is a file in Linux). They refer to the current directory file and parent (up) directory file respectively.