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
The output will be the complete path of the current working directory like below.
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 naviagate to the
/etc/security/ directory with a single command.
$ cd /etc/security/
Alternatively the cd command can be used two time in order to navigate to the
$ cd /etc $ cd security
The relative path is expressed as related to the current working directory or parent directory using double dots.
The relative path can be used to change 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 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 go upper directories and directory names will be used to go down to child directories.
$ cd ../ismail/../
Navigate To Previous Directory
The cd command provides the dash character, – as an parameters. The dash is used to specify the previous directory which can be a parent, child or unrelated directory.
$ cd -
Navigate To 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 users home directory only cd command can be executed like below. There is no need for extra parameter.
Navigate with Directory Name with Spaces
Directory names or path can provide spaces. If these spaces are not handled properly this creates errors using with the cd command. The most stable way to use cd command with directory names and paths containing space is using single or double quoto. Just surrount the directory name or path with single or double quoto.
$ cd "/home/ismail/My Downloads"
It is the same using single quoto for the directory names and path with spaces.
$ cd '/home/ismail/My Downloads'
Alternatively the back slash can be used to express the space without using the single or double quoto. But using back slash is not easy to read and understand which results with 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 ew can use the cd command mutliple times by using
$ cd Downloads; ls; cd ..;