The UNIX/Linux CD command is popularly used to move into different directories from the current working directory using the command line (or terminal).
When you open your terminal, Linux will use your home directory as the current working directory, so to navigate into a different directory, you can use the cd command.
|Description||CD (Changing Directory)|
|Root or Sudo Privileges||No|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
Syntax of the CD Command
The cd command takes two arguments: one is the option, and the other is the directory path.
$ cd [OPTION] [DIRECTORY]
The following are the known options for the cd command:
|Force following symbolic links (enabled by default).|
|Instead of symbolic links, specifying this option will follow the physical directory structure of the user-requested directory.|
|If the “-P” and current directory could not be determined successfully, exit with a non-zero status.|
|Present a file with extended attributes as a directory containing the file attributes.|
Working Scenario in this Article
For new Linux users, you can check the following tree map of a Linux filesystem with multiple directories and nested directories that I will use to show you the usage of the cd command.
Considering that, let’s move on to the practical.
Go to the Specified Directory
1. Execute the following command to move into the “
$ cd Documents/
2. Execute the following command to move into the “
/var/www/html” directory from your current directory.
$ cd /var/www/html
3. The following command will move you into the parent directory of the current directory (it’s one step above).
$ cd ..
4. To go back to the previously active directory, use the “
–” dash symbol, as shown.
$ cd -
5. Enter into another user’s home directory (ex: “
jake“) by specifying the home directory name with the “
~” tilde symbol.
$ cd ~jake
6. Go to the root directory from the current working directory.
$ cd /
7. Move into your own home directory by using the following command.
$ cd #OR $ cd ~
8. Enter the directory with a space.
$ cd Documents/'directory with space'/ #OR $ cd Documents/directory\ with\ space/
9. Switch to another directory using the current working directory.
$ cd ../proj/scripts/
10. Move two directories from where you are now.
$ cd ../../
11. Move into another directory from your current working directory.
$ cd ../Downloads/
12. Navigate to another directory without specifying its name using “Tab” key.
$ cd ../Doc<TAB>/p<TAB>/s<TAB>/
Not that the “
<TAB>” string is used to refer to the tab key on the keyboard, and also, the first character of the directory name you are specifying should be unique; if any other directory with the same character exists, then specify a few more characters.
13. Move into the first directory that starts with the letter “
Dow” in your home directory.
$ cd ~/Dow*
Note that in home directories, there are usually three directories that start with “D” (Desktop, Documents, and Downloads).
That is why, instead of “D“, two extra letters “ow” are specified to be more specific.
14. Put the current directory on the stack and move into another directory.
Pushd and popd commands are used to stack the present directory in memory and move into another directory.
When the job is done, fire the popd command, and the current location will change back to the stacked directory in memory.
For example, use the pushd directory to move into the “
$ pushd /var/www/html/
Now do what you want, and when the job is done, execute the popd command, and you will be moved to the stacked directory in your memory.
15. Listing all the content of the directory after moving into it.
$ cd ~/Documents/ && ls
That’s all for now.
If you have a question or an example that should be included in this article, feel free to tell us in the comment section.