cd is the basic command used by the Linux users. It is used to change the current working directory of a shell.
Syntax:
cd [-L|[-P [-e]] [[email protected]]] [dir]
Before reading this tutorial, you must know few basics about directories on a Linux system. Get the basic understanding about the absolute vs relative paths in Linux.
- Absolute Path is the full path of directory and always starts with / in Linux system. For example: /tmp, /usr/bin, /var/www etc.
- Relative Path is the partial path of any directory or subdirectory. It can be started from any location of file path except /. For example: www, ./script.sh, ../../www etc.
Now you must memorize these symbols and special characters use the cd command. What’re the symbols meaning when used with the cd command.
- Single dot (.) : current directory (present directory).
- Double dot (..) : parent directory.
- Tiled (~) sign : home directory of current users.
- $HOME variable: home directory of current users.
cd Command Examples
Here is more examples to use with cd commands on Linux system.
- Sample cd command – Provide the remote directory path to switch current directory location of the working shell.
cd /opt
- Using absolute directory path – Any directory path started with the root (/) file system is known as absolute directory path. In other words, you can say it full path.
cd /opt
cd /data/backup
cd /var/www/html
All the above examples, uses directory location with root (/) are the absolute directory paths.
- Using relative directory path – Any directory path refers from current directory is called relative path. The relative path never started with root (/) file system.
cd backup
cd www/html
In the above example, first command will change change to “backup” directory available under current directory. The second command will change to “www/html”, which is located in current directory.
- Home directory – You can also use $HOME environment variable for navigation useful for the scripting. Here $HOME always refers to the home directory of the logged in user.
cd $HOME
cd $HOME/public_html
The “~” also refers to the logged in user home directory and can be used with cd command.
cd ~
cd ~/public_html
- Using . and .. in directory path – The . refers to the current directory and .. refers to the parent directory except /root. This is generally used with relative paths.
cd ./public_html
cd ../backup/files
The first command is same as “cd public_html” where second command navigates shell to backup directory available under parent directory.
We can also use multiple .. (as much as required) to navigate to any directory structure.
cd ../../../backup/files
Conclsion
This tutorial descirbes you to uses of cd command on Linux system.