cd command in Linux is quite common command and basically used in for changing the working directory. Even the full form of cd stands for Change Directory, and this command can be used on all Unix-like operating system, even in Windows while using Command Prompt.
Technically, cd command is a shell built-in command and its behavior may differ from shell to shell, and in this article, we are going discuss the Bash built-in version of cd command.
Table of Contents
Syntax to use cd command in Linux
cd [Options] Directory
Options available to use with cd command in Linux
- -L: Using this option, the cd command follows the symbolic links, and also this option is default.
- -P: With this option, the cd follows no symbolic links, and when this option is used, it indicates the user is attempting to navigate to a symlink which points to a directory.
Means, if you just use cd without any argument, it will take you back to your home directory. For an example, if you type ‘cd /var/www’, your working directory will be ‘www’. But, when you type just ‘cd’ while working in current directory, it will take back to home.
About Absolute and Relative Path Names
While using cd command, the Absolute path basically means for full path which starts from system root which is referred as /, while the relative path starts from the directory in which you are working currently.
When you start the terminal in Linux system, your currently working directory is set to home directory. And obviously, the Downloads directory lies within home directory, and you can navigate to it by using just relative path to directory.
For example, suppose your currently directory is home, and want to change your directory to Downloads, you just need to type:
In this command, we used Relative Path to change working directory to Downloads. However, the same thing can also be done using Absolute Path by running this command:
In the above examples, it simply indicates that if the path to directory starts with /, it’s an Absolute path, else Relative path.
About Parent Directory
While using terminal, the current working directory is indicated as single dot (.), where as the Parent Directory is indicated as two dots (..). Parent directory basically means the directory in which the current working directory lies.
For an example, if you are working in /var/www directory, and type cd command by using single dot (.) as argument, it will remain in the same directory, or say it will do nothing.
But, when you type below command, you will switch to one level up from the current directory that is /var.
Also, you can switch the directory to two level up directory by running this command.
Navigating to Previous Working Directory
In order to navigate back to a directory where you were working previously, you can use dash (-) as an argument with cd command. Means, suppose you choose to work in /var/www directory. You can do this by typing:
Now, you want to go back to parent directory /var, then you will run:
And, here again you want to switch back to previous working directory /var/www, then you can easily do this by using – with cd command:
Navigating to Home Direcotry
As we discussed above, we can simply type cd without any argument to go back to home directory. Also, this can be done by using tilde (~) as an argument.
In order to navigate to other user’s home directory, you can use this command:
Navigating to directories with spaces in name
It’s very common to face off this condition to change to directory which has spaced in its name. However, doing this is very simple as it just needs to surround the path with quotes or use backslash (\).
cd 'servo node' or cd servo\node
Nishant Verma is a senior web developer who love to share his knowledge about Linux, SysAdmin, and more other web handlers. Currently, he loves to write as content contributor for ServoNode.