The cd command is the most basic command for working with directories in the shell. That’s because it’s used so often. Any time you need to change locations in a file system, you’ll be using a cd to do so. You might not know this, but the cd (change directory) command has many different uses and applications that extend beyond simply changing your current working directory.
In this article, we will introduce you to these various uses of the cd command in Linux and become familiar with its most common usages.
Syntax:
cd [-L|[-P [-e]] [-@]] [dir]
Before continuing, you should get a basic understanding of 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 the file path except
/.
For example: www, ./script.sh, ../../www etc.
Now you must memorize these symbols and special characters and use the cd command. What’s the symbol’s 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 are more examples to use with cd commands on the Linux system.
- Sample cd command – . Provide the destination directory path to switch the current directory location of the working shell. For example, change to the /opt directory:
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 its full path.
cd /opt
cd /data/backup
cd /var/www/html
All the above examples use 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 the root (/) file system.
cd backup
cd www/html
In the above example, the first command will change to the “backup” directory available under the current directory. The second command will change to “www/html”, which is located in the 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 the same as “cd public_html” where the second command navigates the shell to the backup directory available under the parent directory.
We can also use multiple .. (as much as required) to navigate to any directory structure.
cd ../../../backup/files
Conclsion
Change directory is the most used command in Linux systems. In this tutorial, we have described the cd command on a Linux system.