An easy way to change directories in Linux from the terminal
Some commands in Linux are so frequently used that we often overlook the significance of the commands and the details about them get missed.
cd is one such command.
cd stands for ‘change directory’ which itself explains its use and purpose.
cd allows you to easily change your current directory to whichever directory you wish to move. Just put the right path in the command and you will be placed in that directory by
In this brief tutorial, you will get all the basic and useful insights into the
cd command-line utility.
Knowing more about the cd command
cd command is a useful utility for all the frequent command-line users and also who are required to manage GUI-less servers.
Let us look at the basic syntax of the
cd [options] [directory_or_directory_path]
The following table will give you a brief insight into what happens when you use these options with the
||changes the present directory to the root directory|
||changes the directory to the home directory|
||Represents the current directory|
||change to the parent directory of the current directory|
cd: You can change your directory directly by enetring the name of the destination directory.
Here, we have simply changed the current directory to a directory named ‘workspace’.
Note: Please note that this workspace directory should be placed in your present working directory. If it’s not, then you will get an error. You can use the complete path of the desired directory with the
cd command. We will be learning about this in the upcoming examples.
cd / : This command will change your current directory to the root directory.
[email protected]:~/workspace$ cd / [email protected]:/$
Here, we have changed the current working directory from ‘workspace’ to
[email protected]:/$ pwd / [email protected]:/$
On using the
pwd (print working directory) command the ‘
/ ‘ (root) directory is displayed.
cd ~: This command takes you back to the home directory from whichever directory you might be working into.
[email protected]:~/space/apache$ pwd /home/gaurav/space/apache
I am currently in the directory named apache. Let us now use the
cd ~ (tilde) command.
[email protected]:~/space/apache$ cd ~ [email protected]:~$
[email protected]:~$ pwd /home/gaurav [email protected]:~$
Now, we are back to the home directory ‘/home/gaurav’.
cd ..: This command allows you to change your current working directory to the parent directory one level above your present directory.
[email protected]:~/snap/htop/1332$ pwd /home/gaurav/snap/htop/1332 [email protected]:~/snap/htop/1332$
In this example, /home/gaurav/snap/htop/1332 is the current working directory path. We are actually into the directory 1332. The immediate parent directory of the ‘1332’ directory is ‘htop’ directory. On using the
cd .. command, we will move to the ‘htop’ directory, its immediate parent directory.
[email protected]:~/snap/htop/1332$ cd .. [email protected]:~/snap/htop$
[email protected]:~/snap/htop$ pwd /home/gaurav/snap/htop [email protected]:~/snap/htop$
Above given are some useful options used with the
cd command. Now, let us dive into some more detailed examples of the
Changing from current directory to a specific path
You can use the
cd command, to change to any directory using its path.
Here, we wish to change to a directory named ‘examples’ placed at the path
/home/gaurav/snap/htop/1332/examples from the home directory.
Note: Here, I have used
./ instead of typing in the complete path of my home directory. You can learn more about it in this article.
[email protected]:~/snap/htop1332/examples$ pwd /home/gaurav/snap/htop/1332/examples [email protected]:~/snap/htop/1332/examples$
We are now placed in the directory ‘examples’.
Moving to directories with white-spaces in their name
There are many instances when we use ‘spaces’ while naming the directories. Sometimes, just using the
cd command with the names of this type, doesn’t seems to work. But there’s a simple fix for this.
Putting the directory name inside single quotes or double quotes can solve the problem. You can simply use
"directory name" or
cd 'directory name'.
cd "directory name 22"
cd "Calibre Library"
[email protected]:~$ cd "Calibre Library" [email protected]:~/Calibre Library$
[email protected]:~/Calibre Library$ pwd /home/trinity/Calibre Library
We are now shifted to the Calibre Library directory which had white-space in its name.
Changing back to the previous directory
We previously saw the use of the
cd .. command, which takes you to the parent directory of your current working directory. Here we will look at one more alternative for this.
cd - (dash) command allows you to perform more or less the same action. You can move to the previous directory of your current working directory.
[email protected]:~/workspace/snap/vim-editor$ pwd /home/gaurav/workspace/snap/vim-editor [email protected]:~/workspace/snap/vim-editor$
Here, I am currently working in the ‘vim-editor’ directory. Suppose a user wishes to move to the previous directory, then
cd - can be helpful. Let’s see how.
[email protected]:~/workspace/snap/vim-editor$ cd - /home/trinity/workspace/snap [email protected]:~/workspace/snap$
Here, we have now moved to the previous directory named ‘snap’.
In this super simple tutorial, we learned about a very basic and friendly command
cd (i.e change directory) used in all the Linux systems. We will now be able to navigate through multiple directories while working on the terminal without using the GUI.
cd command will be easy to use after going through this tutorial.