The cd
command is used to change the current working directory and navigate between different directories. The cd is the short form of the change directory term. The cd command is one of the most used commands in Linux.
cd Command Syntax
The cd command has the following simple syntax.
cd OPTIONS PATH
- OPTIONS is used to provide different options. The OPTIONS parameter is optional.
- PATH is used to navigate to the specified path which can be relative or absolute. This PATH parameter is optional.
Current Working Directory
Before starting the examples of the cd command for navigation on the command line we should learn. The current working directory or current working path is the active path where the specified command will run. Before changing the current working directory we can display the current working directory. The pwd command is used to
$ pwd
The output will be the complete path of the current working directory like below.
/home/ismail
Absolute Path and Relative Path
In Linux, paths can be specified in two forms. The absolute path is the path specification where the complete hierarchy of the path is provided completely by starting from the root directory. Below you can see some absolute path examples.
/home/ismail
/var/www
/etc
/var/log/syslog
Navigate to Directory
The cd command can be used with the absolute path like below. In the following example, we navigate to the /etc/security/
directory with a single command.
$ cd /etc/security/
Alternatively, the cd command can be used two times in order to navigate to the /etc/security
directory.
$ cd /etc
$ cd security
The relative path is expressed as related to the current working directory or parent directory using double dots.
ismail/Downloads
../etc/security
The relative path can be used to change the directory and navigate to the other directories. In the following example, we will navigate to the current working directory Downloads folder, and DEBIAN folder.
$ cd Downloads/DEBIAN/
Navigate To the Parent Directory
The bash terminal provides the double dots or .. in order to specify one level upper directory which is called parent directory. For example, the following cd command with double dots will navigate to the one-level upper directory which is /home/ismail .
$ cd ..
We can also use the double dots multiple times in a single path. In the following example, we will navigate to the 3-level upper directory.
$ cd ../../../
The double dots can be also used with the directory names together where double dots will be used to upper directories and directory names will be used to go down to child directories.
$ cd ../ismail/../
Navigate To the Previous Directory
The cd command provides the dash character, – as a parameter. The dash is used to specify the previous directory which can be a parent, child, or unrelated directory. We can also call this as “cd to the previous” directory.
$ cd -
Navigate To the Home Directory
Linux users have home directories in order to store personal files and folders. The home directory is generally located under the /home . The tilda ~ can be used to specify the current user home directory. For example, if the current user is ismail the ~ will be /home/ismail . Tilda can be used with the cd command easily.
$ cd ~
We can also use the Tilda with child directories like below.
$ cd ~/Desktop
Also, another user’s home directory can be navigated by using the tilda and user name. In the following example, we will navigate to the ahmet home directory.
$ cd ~ahmet
Alternatively, in order to move to the current user’s home directory, only the cd command can be executed like below. There is no need for extra parameters.
$ cd
Navigate with Directory Name with Spaces
Directory names or paths can provide spaces. If these spaces are not handled properly this creates errors using the cd command. The most stable way to use cd command with directory names and paths containing space is using a single or double quote. Just surround the directory name or path with a single or double quote.
$ cd "/home/ismail/My Downloads"
It is the same using a single quote for the directory names and paths with spaces.
$ cd '/home/ismail/My Downloads'
Alternatively, the backslash can be used to express the space without using a single or double quote. But using a backslash is not easy to read and understand which results in errors.
$ cd /home/ismail/My\ Downloads
Use Multiple cd Commands in A Single Bash Line
By default, only one cd command can be executed in a single bash line. But if we need we can use the cd command multiple times by using ;
.
$ cd Downloads; ls; cd ..;
Navigate To Root Directory
We can navigate to the root directory with the following command. The root directory is /
.
$ cd /
Navigate Two Directory Up with “cd ../..”
As stated previously the ..
is used to point upper level or parent directory. we can use the .. multiple times in order to navigate to cd to two directories up.
$ cd ../..