Linux CD Command with Examples

cd command linux

In Linux, the cd command is one of the most widely used commands. While most users limit their use of the cd to the basic, the cd command has a wide range of other features. Change Directory (cd) is a command that allows you to easily switch from the current directory to any other directory.

In this tutorial, we will discuss the cd command-line utility in detail with examples.

cd Command

cd is a built-in shell that uses the shell environment variable to determine what information is required for the operation. It is used to change the current working directory.

When navigating to a file system, you can use the Tab key to autocomplete the directory names. You must have executable permissions on a directory to switch to that directory.

Syntax:

$ cd [options] [path]

The options in the syntax represent the argument passed with the command and the path is the absolute or relative path directory.

When you use the cd without placing any arguments, that cd command will take you to your home directory.

Syntax:

$ cd

For Example:

kali@kali:~/Desktop/Sublist3r$ cd
kali@kali:~$ 

Here, we navigated from the folder to the home directory.

Absolute and Relative Pathnames

You can use either absolute or relative pathnames to define a directory you need to change to. You must know the difference between them before you can use the cd command to its full extent.

The absolute pathname is a complete path to the directory starting with the /(root) folder whereas the relative pathname is derived from the current working directory.

By default, your current working directory is set to your home directory when you connect to your Linux system.

Assume you are in your home directory and want to change the directory to /Downloads. We can navigate to the /Downloads directory in two ways: using the relative path or the absolute path.

In the relative path, simply type cd along with pathname without a slash (/)

Syntax:

$ cd Downloads 

It is an absolute path to the directory if the path is given using a slash (/).

Syntax:

$ cd /home/username/Downloads/

Output:

kali@kali:~$ cd /home/kali/Downloads/
kali@kali:~/Downloads$ 

Switch to Home Directory

The /home directory on a Linux system contains a user's personal files, applications, and subfolders. You can simply run the cd command without passing any arguments if the $HOME environmental variable is set to your home directory path. If not then, the home directory has a tilde (~) character assigned to it. This can be passed with the cd command to change the current working directory to /home without specifying the complete path of your home directory (/home/username).

To return directly to your home directory, the following command can be used.

Syntax:

$ cd ~

Output:

kali@kali:~/Downloads$ cd ~
kali@kali:~$ 

Change to other user's home directory

Syntax:

$ cd ~[username]

Output:

kali@kali:~$ cd ~linux/
kali@kali:/linux$ 

Change to Downloads directory which is inside the home directory

$ cd ~/Downloads 

Output:

kali@kali:/linux$ cd ~/Downloads/
kali@kali:~/Downloads$ 

Switch to Root Folder

In your file system, the root directory is the first directory. The slash (/) character is used along with the cd command to navigate to the root directory.

Syntax:

cd /

Output:

kali@kali:~/Downloads$ cd /
kali@kali:/$ 

Change to Previous Working Directory

When you are working with multiple directories at once, the hyphen (-) character can be used to quickly switch to the previous working directory.

Syntax:

$ cd -

Output:

kali@kali:~/Desktop/Sublist3r$ cd -
/home/kali/Desktop
kali@kali:~/Desktop$ 

Switch to Parent Directory

The current working directory is represented by a single dot (.) in Linux operating systems. The parent directory, or the directory above the current one, is represented by two dots (..). The parent directory, in simple terms, is the directory that contains one or more sub-directories.

If you write cd . command, it will change to the current directory which means that the path will remain the same.

The cd command along with double dots (..) allows you to change your current working directory to parent working directory one level above your present directory.

Syntax:

$ cd ..

Output:

kali@kali:/usr/local/lib$ cd ..
kali@kali:/usr/local$ 

To move two levels above your working directory, the following syntax can be used.

Syntax:

$ cd ../../

Output:

kali@kali:/usr/local/lib$ cd ../../
kali@kali:/usr$

You can also specify a directory one level above the current working directory.

Syntax:

$ cd ../[folder]

Output:

kali@kali:/usr/local$ cd ../sbin/
kali@kali:/usr/sbin$ 

Change to Directory Name with Spaces

Every folder on your system will not be named with a single word. If it contains a space character, specifying the directory name will result in an error.

For example:

$ cd /home/kali/Desktop/Important Files

Output:

kali@kali:~$ cd /home/kali/Desktop/Important Files
 bash: cd: too many arguments
 kali@kali:~$ 

To avoid the error, either enclose the path in quotes or use the backslash (\) character to escape the space.

Using Backslash (/)

For Example:

$ cd /home/kali/Desktop/Improtant\ Files/

Output:

kali@kali:~$ cd /home/kali/Desktop/Improtant\ Files/
 kali@kali:~/Desktop/Improtant Files$ 

Using quotes (' ') (" ")

For Example:

$ cd /home/kali/Desktop/'Improtant Files'/
$ cd /home/kali/Desktop/"Improtant Files"/

Output:

kali@kali:~$ cd /home/kali/Desktop/'Improtant Files'/
 kali@kali:~/Desktop/Improtant Files$ 
 kali@kali:~/Desktop/Improtant Files$ cd
 kali@kali:~$ cd /home/kali/Desktop/"Improtant Files"/
 kali@kali:~/Desktop/Improtant Files$ 

Conclusion

In this tutorial, we learned about the cd command and how to use it to navigate through multiple directories. It enables you to quickly switch to different directories using different characters and arguments.

Leave a Comment