
You can use cd
(“change directory”) command to switch the current working directory in Linux and other Unix-like operating systems. This command is used frequently and it’s basic command to work with Linux terminal. In this guide, we will show you how to use the cd command to navigate your system’s directory tree.
cd Command (Change Directory)
cd
is a builtin command and following is the syntax for the cd
command:
cd [OPTIONS] directory
You can pass options which are not mostly used.
−L
, By passing it follow symbolic links. By default, cd behaves as if-L
option is specified.−P
, It will not follow symbolic links. In simple words, when you try to navigate symlink it will change to directory which is pointed to it.
If you will not pass any argument cd
command will navigate to home
directory.
To switch to a directory, you must have executable permissions for that directory it. The pwd
command allows you to find out current directory.
Absolute and Relative Path
You can change directory using either absolute (full path) or relative path. Full path starts from the root of system /
and relative path starts from your working directory. In short, if the path starts with a slash (/)
it is the absolute path to the directory.
By default your working directory is home directory. If you would like to navigate to Documents directory you can do it by simple relative path:
cd Documents
Also, you can use absolute path to navigate same directory:
cd /home/tecnstuff/Documents
The Parent Directory
In the Unix distro single dot (.)
represents the current directory. Two dots (..)
represents the parent directory or in other words the directory immediately above the current one.
For example currently you are in /var/www/html
directory and you would like to switch to /var/www
directory, you can using:
cd ../
To go to two level up means to parent’s parent you have to type:
cd ../../
Another example, you are in /var/www/html
directory and you would like to switch to /var/www/log
. You can do it by simply typing:
cd ../log
Navigate to the Previous Directory
To switch to previous working directory, just pass dash -
character as an argument along with cd
command:
cd -
Navigate to the Home Directory
You can directly land to home directory using cd
command. Alternate way is to pass tilde ~
character as an argument.
cd ~
For example, if you would like to navigate to Documents
directory you can use below command:
cd ~/Documents
Same as you can navigate to another user directory using following command:
cd ~username
Directories with Space in Their Names
If the directory name contains space
then you should enclose the name with quotes. Either you can use backslash (\)
character to escape the space.
cd 'Test Folder'
cd Test\ Folder
Conclusion
Now you successfully learnt how to use cd command in Linux. We also show you how to navigate to parent directory and working directory.
If you have any query or suggestion, you can comment below.
Leave a Reply