Linux cd

Linux cd: As in GUI operating systems like Windows 10, we use the mouse to travel to various folders back and forth; similarly, we have the “cd” command in Linux operating systems. This cd command stands for change directory, which also explains the use of the command.

The syntax is similar to any other command in Linux, “cd <option> [argument]” is the general syntax.

This command is used in various ways according to the user requirement. Here we have discussed various ways to use the cd command with several arguments.

 Example 1

In this example, we have used “cd” to change our current directory and move to a new specific directory. The specific directory is the desktop of the user.

 #pwd
#cd Desktop/
#pwd 
Linux cd

Example 2

In this example, we are going to change our directory using the absolute path. First, we must know what an absolute path is? An absolute path is the location or path of any file or directory derived from the root directory. This root directory in the Linux system is “/.” So let us now use the same location used in the above example but with an absolute path description.

 #pwd
#cd /home/tutorialandexample/Desktop/
#pwd 
Linux cd

The highlighted rectangle shows the absolute path used to change the directory to the desktop.

Example 3

In this example, we are going to learn about jumping from the current directory to the home directory of the user. We have two ways or commands to do this process: one is by using the tilde “~” symbol, and another is without any symbol. The below screenshot shows the implementations for both commands.

 #pwd
#cd ~
#pwd 
Linux cd

Both the commands are performed at /bin as the current working directory.

 #pwd
#cd
#pwd 
Linux cd

Example 4

In this example, we have tried to show the case of jumping to the root directory. The root directory is denoted by “/” in the Linux system. All the other directories and files are stored in the root directory.

 #pwd
#cd /
#pwd
#ls 
Linux cd

It would often be best if you went to the root directory to change some configuration or to command other users on the system.

Example 5

In this example, we have shown how to change the directory to the home directory of any specific user. Suppose you have many users on the system, and you are the admin who wants to know the work done by the employee. You can directly jump to the home directory of that specific user and check all the work.

 #pwd
#ls
#whoami
#cd ~hemant
#pwd 
Linux cd

In the above screenshot, we are at the home directory and listed the users. We have two users, “hemant” and “tutorialandexample,” and we checked the current username with the help of the “whoami” command and then changed the current directory to the home directory of the user “hemant.”

Example 6

This is a fascinating example; here, we are going to learn about the directory names with spaces in them. For example, “this is a directory Name,” in this case, we have to change the directory. The conflict in these directory names arises due to the syntax of the commands in the Linux system. The CLI interface takes arguments and options separated by a space, and if you use a directory name with spaces, it gets confused.

 #mkdir ‘this is a directory Name’
#ls
#cd this is a directory Name 
Linux cd

In the above screenshot, we first created a directory named “this is a directory Name” and then tried to jump to the same directory. But, we got an error of too many arguments. To resolve this issue, the Linux CLI provides us a solution to represent the spaces present in the directory's name.

 #mkdir ‘this is a directory Name’
#ls
#cd this\  is\  a\  directory\  Name/
#pwd 
Linux cd

The pwd command in the above screenshot shows that the command was successful, and we are currently inside the directory.

Example 7

In this example, we have shown the way to jump multiple subdirectories.  To do this, you only need to add the forward-slash (/) after every directory you want to jump or change. This is a hierarchical jump, and all the directories should be in a hierarchy.

Linux cd

The highlighted portion shows the hierarchy of the directories; the syntax is “cd </dir1/dir2/dir3>” in this command.

Example 8

In this example, we have shown the command to jump to the parent directory. This command is very similar to the “back” button in Windows systems. This command will bring you to the parent directory of the current directory. If you directly want to come to the home, we have already explained that.

Linux cd

The above screenshot shows that we are at the home directory. And then, we tried the command “cd ..” and changed our directory to the parent directory. We performed this several times, and at last, we got to the root directory. We again tried the command at the root directory, but nothing happened because the root directory is the parent directory to all the directories present in the Linux system.