Today you will learn how to change the directory in Linux using the cd command.
‘cd‘ stands for “Change Directory“.
The basic use of the cd
command is to change the current working directory and this is one of the most frequently used commands.
These commands are extensively used in shell scripting.
These basic commands are very important to learn for those who are beginners and just started learning Linux.
Before going on to the topic, there are some things that you should understand and that is Absolute Path and Relative Path.
The top most directory in the file system is the root directory and We denote the root directory with a slash(/
).

You can describe the location of any file or directory in the file system, with a full path beginning with a slash(/
).
The full path is called the Absolute Path and that means that it tells every step that has to be taken from the root or the absolute beginning of the file system.
You can use the pwd
command to check the absolute path of a directory. Here is an example of an absolute path.
# pwd
/root/data/accounts/files
Absolute Path is decided and easy to understand but they can also be inconvenient to work with especially if you are working with deeply nested directories.
To make things simpler, we can use Relative Paths instead. A file’s relative path is its location relative to the current working directory.
If you are working with files in or near the current working directory, this can save you a lot of typing.
Every time you have referred to a file by just its name you’ve been using a relative path. This is the simplest sort of relative path.
Let us take an example. Here I have a directory tree.
# tree data/
data/
└── accounts
├── documents
│ └── data.txt
├── file1.txt
├── file2.txt
├── file3.txt
├── file4.txt
├── file5.txt
└── files
According to the above directory tree, If the current working directory is accounts then the relative path of the documents directory is just documents.
Rather than having the full path from the root, we can just have the path from the working directory.

You can also write relative paths for files. For example, the relative path of the data.txt file and the documents directory is just documents/data.txt
.
Notice that unlike a full path, the relative path does not start with a slash(/
).
That’s how you can tell a relative path from an absolute one. You can also write relative paths for items that are closer to the root.
The special directory entry ‘..
‘ points from the directory to its parent.
So if you are in /root/data/accounts/documents
and you refer to ‘..
‘ you are talking about the directory /root/data/accounts
.
There is also a special entry which is just ‘.
‘.
‘.
‘ points from each directory to itself or the current directory.
For example, because our current directory is accounts, if we use ‘.
‘ that refers to /root/data/accounts
.
These special directory entries can be displayed under the command ls -a
Which helps in listing hidden files and directories.
$ ls -a
. .bash_profile data .esd_auth Music Templates
.. .bashrc Desktop .ICEauthority Pictures Videos

Suggested Read:
Another handy shortcut is ~
(tilde) is an abbreviation for your home directory.
By starting a relative path with ~
you can easily specify paths relative to your own home directory.
The following command will change from the current working directory to /home/helpdesk/Desktop
.
# cd ~/Desktop/
I hope now you have understood what is Absolute Path and Relative Path.
So let’s get to the topic.
You must follow the syntax given below to use the cd
command.
cd [OPTIONS] directory
1. Change directory using cd Command
To switch from the current working directory to data/accounts/files/
type the following command.
In this example, I have changed the directory using a relative path.
# cd data/accounts/files/
You can also take another example above this scenario.
If your current working directory is data/accounts/
, then type the following command to enter the data/accounts/files/
directory.
# cd files/
And if you want to change the directory using the absolute path, you need to write the command something like this.
# cd /root/data/accounts/files/
2. How to change a directory that has white space in its name
We can use the following methods to change a directory that has white space in its name.
Here in this example, I have a directory named “my database
” which I want to change using the cd command.
Method #1
# cd my\ database/
Method #2
# cd "my database"/
Method #3
# cd 'my database'/
3. Change from current working directory to parent directory
To change from the current Working directory to the parent directory issue the following command.
Suppose my current working directory is /root/data/accounts/files
and I want to switch to its parent directory ie /root/data/accounts
.

Note: You can print the current working directory using the pwd
command
So my current working directory is :
# pwd
/root/data/accounts/files
Now type the following command to switch to its parent directory.
# cd ../
After switching, my current working directory is this.
# pwd
/root/data/accounts
4. Switch to as many upper level directories
You can switch to as many upper level directories as you want with the cd
command.
For which you have to use ..
with the cd command. Let’s take an example.
My current working directory is /root/data/accounts/files
and I want to switch to two top level directories.

The following command will switch to two top level directories.
# cd ../../
Let’s take another example that will make your concept more clear.
My current working directory is this : /home/helpdesk/data/accounts/files
. Now the task is to change from the current working directory to /home/helpdesk/Desktop
.
There are two ways to accomplish this task. Either you can complete this task by using the relative path.
# cd ../../../Desktop/
Or one can also use the complete path i.e. Absolute Path.
$ cd /home/helpdesk/Desktop/
It is up to you what method you want to use. In both cases, the output will be the same.
$ pwd
/home/helpdesk/Desktop
5. Change to Home Directory
As I have told you at the beginning of the article ~
(tilde) is an abbreviation for your home directory.
That’s why you do not need to mention the entire path to switch the user’s home directory from the current working directory.
Let’s take some examples on this.
To change from the current Working directory to the user’s home directory type the following command.
# cd ~
Or you can go to the home directory by running the cd
command.
$ cd
$ pwd
/home/helpdesk
Run the following command to go to the user’s desktop.
# cd ~/Desktop/
The following command will switch from the current directory to another user’s home directory.
In this example, I am switching to the home directory of a user named linuxadmin.
# cd ~linuxadmin/
# pwd
/home/linuxadmin
6. Switch to the previous working directory
To change from the current Working directory to the previous working directory issue the following command.
# cd -
Help/Manual page access
Use the following commands to access the Help Page of cd
command.
# cd --help
You can visit at following websites to get more information on cd
command.
Conclusion
I hope you have learned something from this article.
I have tried my best to include all the features of cd command in this guide.
Now I’d like to hear your thoughts.
Was this guide useful to you?
Or maybe you have some queries.
Have I not included any command in this guide?
Leave a comment below.