×

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.


Related Topics

chmod Command in Linux/Unix with Examples

The chmod command in Linux is a command-line utility that is used to change the permissions of files and directories. It stands for "change mode" and it is used to...

4 minutes read.

telnet Command in Linux/Unix with Examples

The telnet command in Linux is a command-line utility that allows you to connect to a remote host using the Telnet protocol. Telnet is a network protocol that provides a...

3 minutes read.

How to change the ApacheHTTP port in Linux

What is an Apache HTTP server in Linux? When we talk about the most commonly used web servers, one of the web servers that cannot be missed is Apache. With the...

4 minutes read.

tail Command in Linux/Unix with Examples

The "tail" command in Linux (and other Unix-like operating systems) is used to display the last few lines of a file. By default, the tail command will display the last...

4 minutes read.

Linux Commands List

Linux Commands List The Linux systems are filled with commands for several use cases. We have divided these commands into some of the most common implementations for a better understanding of...

2 minutes read.

cp Command in Linux/Unix with Examples

The cp (copy) command in Linux is a command-line utility that is used to copy files and directories. It is one of the most basic and commonly used commands in...

5 minutes read.

ps Command in Linux/Unix with Examples

The 'ps' command in Linux is used to display information about the running processes on a system. The command stands for "process status" and it allows you to view details...

4 minutes read.

awk Command in Linux/Unix with Examples

The 'awk' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

4 minutes read.

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...

4 minutes read.

gzip Command in Linux/Unix with Examples

The gzip command in Linux is used for compressing and decompressing files. It uses the Lempel-Ziv coding (LZ77) algorithm to compress files, which is known for its high compression ratio...

6 minutes read.

find Command in Linux/Unix with Examples

The find command in Linux is a command-line utility that is used to search for files and directories in a file system. It is a versatile command that can be...

5 minutes read.

mv Command in Linux/Unix with Examples

The 'mv' command in Linux is used to move or rename files and directories. The command stands for "move" and it allows you to move files and directories from one...

3 minutes read.

Linux Path Completion

Every operating system has some features which make them special. The Linux operating system primarily works on the command line interface, and we do have many features for this CLI....

1 minute read.

cd Command in Linux/Unix with Examples

The 'cd' command in Linux is used to change the current working directory. The command stands for "change directory" and it is used to navigate the file system on a...

3 minutes read.

cut Command in Linux/Unix with Examples

The "cut" command in Linux (and other Unix-like operating systems) is used to remove sections from each line of a file or stream of data. The cut command is often...

3 minutes read.

wget Command in Linux/Unix with Examples

The wget command is a command-line utility in Linux that is used to download files from the internet. It supports various protocols including HTTP, HTTPS, and FTP and can be...

5 minutes read.

Rename Folder in Linux

Rename Folder in Linux In the Linux system, there is not any single dedicated command for renaming the files and directories. Mostly, the mv command is used extensively for the purpose...

3 minutes read.

ping Command in Linux/Unix with Examples

The "ping" command in Linux (and other Unix-like operating systems) is used to test the connectivity between two networked devices. The "ping" command sends a small packet of data, called...

4 minutes read.

addr2line Command in Linux with Examples

Use of addr2line Command The addr2line command is used to convert addresses to line numbers and file names. This command uses debugging info to check which line number and file name...

4 minutes read.

Linux Absolute and Relative Path

The concept of the absolute and relative path is very simple. The names “relative path” and “absolute path” give us little understanding of the concept we are learning here. The...

1 minute read.