×

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

Difference Between Linux and Windows

Linux vs. Windows Here in this tutorial, we are going to discuss several features of Linux and Windows. These features or characteristics help us to come to a conclusion about which...

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 Admin Tutorial

In publishing and graphic design, lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document without relying on meaningful content....

1 minute 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 apt-get command

Apt-get is a command-line that helps to handle the packages in Linux. Its major task is to assist in retrieving data and packages from trusted sources for purposes of installation,...

6 minutes read.

Linux Commands with Examples

Linux Commands with Examples All the Linux commands are executed on the terminal provided by the Linux system. This terminal is a command-line interface to implement and display command outputs. Linux...

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

Linux Distributions

Linux Distributions Linux OS is not like other operating systems. Linux can be modified according to the user’s requirements, and it is very flexible to any change. Linux distributions are often...

4 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 ls

The primary use case of the ls command is to list the content available in the directory. The default directory is the current directory of the user. The below screenshot...

5 minutes read.

man Command in Linux/Unix with Examples

The man command in Linux is used to display the manual pages for a given command or system call. These manual pages provide detailed information on the command's usage, options,...

3 minutes read.

df Command in Linux/Unix with Examples

The 'df' command in Linux is used to display the amount of disk space available on the file system. The command reports the amount of disk space used and available...

3 minutes read.

addgroup Command in Linux with Examples

What is the addgroup command? It is a command in Linux to create a new group using specified command lined and the system’s default values. How to add groups Adding a user group:...

4 minutes read.

pwd Command in Linux/Unix with Examples

The "pwd" command in Linux (and other Unix-like operating systems) stands for "print working directory." When executed, it simply displays the current directory (or folder) that the user is currently...

3 minutes read.

ssh Command in Linux/Unix with Examples

The ssh (Secure Shell) command in Linux is used to remotely access and control another computer or device over a network. It provides a secure and encrypted connection between the...

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

sudo Command in Linux/Unix with Examples

The sudo command in Linux is used to run a command or set of commands with the privileges of another user, typically the root user. This allows users to perform...

3 minutes read.

netstat Command in Linux/Unix with Examples

The "netstat" command in Linux is a command-line utility that displays network connections, routing tables, and various network statistics. It is used to display the status of network connections, both...

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