×

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

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.

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 SCP Command

SCP stands for secure copy protocol. It is a command-line application for copying files and directories safely between two places. Command-line utilities are programs that operate on a computer's command...

3 minutes read.

Linux Bash

Linux Bash In this tutorial, we are going to learn what the shell is? And how many various shells we have, we have also covered most of the bash shell features....

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

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.

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.

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.

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

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.

du Command in Linux/Unix with Examples

The "du" command in Linux (and other Unix-like operating systems) is used to estimate the space used by a file or directory. The du command stands for "disk usage," and...

4 minutes read.

curl Command in Linux/Unix with Examples

The 'curl' command in Linux is a command-line tool that allows you to transfer data from or to a server using various protocols, including HTTP, HTTPS, FTP, and many more....

3 minutes read.

Linux Tutorial for Beginners

What is Linux? Linux is an open source Operating System like Windows, Android, Mac OS, and iOS. An operating system is a software that enables communication between the user and the...

12 minutes read.

sed Command in Linux/Unix with Examples

The 'sed' 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...

3 minutes read.

Difference Between Unix and Linux

Unix vs Linux Unix and Linux both have been of great use in changing the world of operating systems. Unix was among the very few operating systems which were programmed for...

4 minutes read.

ls Command in Linux/Unix with Examples

The ls command in Linux is a command-line utility that is used to list the files and directories in a directory. It is one of the most basic and commonly...

4 minutes read.

touch Command in Linux/Unix with Examples

The "touch" command in Linux (and other Unix-like operating systems) is used to create new empty files or update the timestamps of existing files. The touch command is a simple...

4 minutes read.

echo Command in Linux/Unix with Examples

The "echo" command in Linux is used to display a line of text on the terminal. It can also be used to print the value of a variable or to...

3 minutes read.