Introduction
The cd command in Linux is one of the most popular and most frequently used commands in Linux and other Unix-like operating systems. cd stands for Change Directory. the cd command is used to change the current working directory in the Linux System.
The current working directory means the directory in which the User currently working. Whenever we want to change the directory we have to use the cd
command.
In this tutorial, we will learn how to use the cd command to navigate in the system’s directories.
cd Command
cd
is a shell built-in, cd command may be differed from shell to shell. cd command requires the shell environment variables to determine necessary information for its execution.
In this tutorial, we will cover the cd command under the Bash built-in version.
Let’s review the syntax of cd
command
cd [OPTIONS] directory
There are only two arguments used with the cd command.
-L
:- cd command by default used the-L
argument to follow the symbolic links.-P
:- cd command with-P
attribute will leave the symbolic link and try to follow the symlink that points to a directory.
If we simply use cd
command without any argument then the cd command will take you to the Home Directory of the current user.
While using the cd command we can use Tab
button to autocomplete the name of the directory. Using the /
at the end of the directory name is not mandatory.
To move to any directory, the User must have executable permission for that directory. For example, a normal user cannot access the root file but the Root User can access root files and Directories by using the cd
command.
pwd
command allows you to find your current location in the Linux file system.
Absolute and Relative Path Names
There are two ways to run the cd
command to change the directory.
Absolute Path – While specifying the directory path, if the path starts from the Root /
then it means the path is an absolute or relative path.
Relative Path – While specifying the directory path, if the path starts from the current path, not from The Root /
then it means the path is a Relative path.
By default, whenever you login into the Linux system, your current directory is set to your home directory.
For example, Assume that the Document
directory exists in your home directory
We can navigate to it by using the Relative path to the directory:
cd Document
We can also navigate to the same directory by using the Absolute path:
cd /home/User_name/Document
Parent Directory
In the Linux operating system, the current working directory is represented by the single dot .
and the previous directory is represented by the two dots ..
If we run cd .
it will navigate to the current directory, in other words, we can say the command will do nothing.
For example, we are in /home/user/Document/Linuxpanda
directory and we have to switch to /home/user/Document/
directory (one level back). Just simply follow the below command:
cd ../
We can also use:
cd ..
If we can switch to from /home/user/Document
to /home/
(two-level back). Just simply follow the below command:
cd ../../
Let’s discuss another example, We want to switch from /home/user/Document
to /home/user/Downloads
. Just simply follow the below commands.
cd ../Downloads/
Navigate to the Previous Directory
To switch back to the previous working directory in Linux System use the hyphen symbol with -
cd command
cd -
Navigate to the Home Directory
To switch to the home directory, simply use the tilde symbol ~
with the cd command. It will navigate to the home directory of the current user.
cd ~
For example, if we want to navigate to the Downloads
directory, which is present in our home directory. Just simply follow the below command.
cd ~/Downloads
If we want to navigate to another’s Home directory. Just simply follow the below command.
cd ~username
Directories with Space in Their Names
If the directory in which we want to switch has spaces in its name then we have to surround the path with quotes or use a backslash symbol \
to escape the space.
cd 'Dir name with space'
cd Dir\ name\ with\ space
Read Also: How to Change Owner and Group of File in Linux Using chown Command
Conclusion
In this tutorial, we have learned about the current working directory and how to use the cd
command to navigate from one directory to another directory. We have seen that with all the possible options we need to use. This command is important for system admins, so practice it as much as you can. Check out more tutorials from our blogs.
If you guys have any queries related to this tutorial, let me know in the comments.
Leave a Reply