One of the common task that you will perform when using the command line interface in Linux, is navigating directories. The Linux file system is hierarchical in nature and the top most directory is called the root and is denoted by the forward slash (/).
The command used to change the current working directory is called cd (Change Directory). It takes exactly one argument which is the path to a folder or directory. The most important concept to remember when using the cd command is the notion of current working directory or PWD.
You are always in a specific directory when using the command line interface. This is called the current working directory. You can find this directory by using the pwd command which prints out the absolute path to the current working directory. The environment variable PWD also holds the value of the current working directory.
navigate to root directory
bash$ cd /
The top most directory of the file system is called the root directory, and the path to root directory is the forward slash (/). This command will leave you in the root folder of your file system. If you meant to go to the home folder of the root user, then that folder is /root/.
bash$ cd /root/
navigate to home folder
The home folder usually refers to the top level directory in the file hierarchy which serves as the personal repository of the user’s personal files. The user has full permissions for this folder and is usually the initial directory that the user is in when they login.
This is also the folder that is the value of the environment variable HOME. So, if you do not specify a command line argument to the cd command, then it uses the value of the environment variable HOME as the default, and you end up in your home folder.
The tilde (~) works as a shortcut that denotes the top level home folder of an user. The default user is the currently logged in user. So, for the currently logged in user, the command below works exactly the same as the previous example.
bash$ cd ~
navigate to the home directory of another user
If you specify the login name of an user (called login id) after the tilde (~) then it points to the home directory of that specified user. This means you can navigate to the home directory of an user named jane using the following command.
bash$ cd ~jane
This works as the starting point if you are trying to navigate down the sub-directories of another user. It goes with out saying that you will need appropriate permissions to do this.
navigate to a specific sub-directory
All of the above examples showed how to navigate to a predefined directory in the Linux file system. If you want to navigate to a sub-directory from the current working directory, then you do that by specifying the name of the sub-directory.
To navigate to the sub-directory named first in the current directory, use
bash$ cd first/
You can specify the path to other directories in the path separated by forward slash (/). The forward slash works as the delimiter between folders (and files) when specifying the path to specific directories. If you have a sub-directory named first and another sub-directory under first named second then you can navigate to second/ using the path first/second.
bash$ cd first/second/
navigate one level up
If you want to navigate to the parent directory then you can use the notation of two dots (..). The parent directory is defined as the directory that is one level up from the current working directory.
If your current working directory is /root/first/second/, then executing the command below will leave you at /root/first/.
bash$ cd ..
navigate multiple levels up
Just as with named directories, you can move multiple levels up by using multiple “two dots” (..) delimited by the forward slash (/). In the example below, you will move three levels up from the current working directory.
bash$ cd ../../..
navigate to the previous working directory
The previous working directory is defined as the working directory before the current command that changed the directory was executed. It is similar to clicking the back button on a web browser.
You use the hyphen (–) as the shortcut to specify the previous working directory. The environment variable OLDPWD holds the value to the previous working directory.
bash$ cd -
navigate using relative path
When navigating to a specific directory on the file system, there are usually two types paths to the directory you can use. One is the relative path and the other is the absolute path. The relative path is relative to the current working directory.
In the example below, you will navigate to a folder that is two levels up from the current folder, and then navigate to a sub-folder named first/ and then sub-folder named second/ under that.
bash$ cd ../../first/second/
You will need to know the current working directory if you are using the relative path to navigate to a folder.
navigate using absolute path
The absolute path to a directory is relative to the root folder (/) of the file system. Irrespective of what your current working directory, the absolute path to a folder is always the same. Using absolute path is useful when writing shell scripts, as you may not be executing the script from the same directory all the time.
bash$ cd /home/jane/first/second/
An absolute path always starts with a forward slash (/) while a relative path does not. The absolute path is useful in situations when you do not have to assume the current working directory.