Linux logo

Linux CD Command: Understand How it Works

What is the Linux/Unix CD command?

The “cd” means ‘change directory’. It is among the most regularly utilized commands in the Linux terminal.

Linux cd command is used to alter the current working directory site (i.e., in which the existing user is working).

There are some techniques that can help you end up being more effective with cd– or you can ditch it, altogether.

The syntax for using the cd command

The general command is:

$ cd [Options] [Directory]

Have a look at a few examples of using the cd command.

An example to go up

You may use the double dots to move up one level. See an example command below:

$ cd ..

If you want to move more level ups, use the double dots as required. For example, moving to two levels up:

$ cd ../..

An example output is shown below. As we ran the pwd command, it displayed the current directory:

Further Reading:  What is Linux ls command?

linux cd command

After that, as cd ../.. executed and then we executed pwd, it went two steps back.

Changing directory using an absolute path example

You have to provide the full path from the root in order to change directory with absolute path option. For example:

/home/user$ cd /home/user/lib
Change directory utilizing a relative path

An example of using the relative path to change directory:

/home/user$ cd lib

Switch back to the previous directory where you working earlier

In order to get back to the directory where you were working, use the dash as shown in the example command below:

$ cd –

In continuation of our above examples:

/$ pwd


/$ ~$ ~$ cd -

~$ pwd

An example of changing to the home directory

To alter the directory to home directory from the present working directory, perform the command as follows:

cd ~
Change to another user’s home directory

We can move to the directory from the existing working directory to a user’s home directory by performing the command as follows:

cd ~username
Change to Directory having Spaces

To change the directory site that has areas in its name, surround the course with quotes (‘ ‘) or use the backslash (\) character with it. Perform the command as follows:

cd 'name of directory with spaces' 

cd Dir\ name\ with\ space
Move to the test folder example
$ cd /usr/local/test

$ pwd

Change to multiple Sub Directories

We can change directory as much as multiple sub-directories by separating directory sites by a forward slash (/) as follows:

cd Folder11/Folder2/Folder3....

Linux cd command is used to change the present working directory (i.e., in which the current user is working). It is one of the most frequently used commands in the Linux terminal.

Further Reading:  Ways to Exit Linux/Unix Vim

Let’s state you’re in the “c” directory. You can utilize cd to alter to the “forth” directory. You can utilize cd – to bounce back and forth in between the two directories etc.