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.
Tutorial Details
Description | CD (Changing Directory) |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | cd |
Internet Required | No |
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:
Options | Description |
---|---|
-L | Force following symbolic links (enabled by default). |
-P | Instead of symbolic links, specifying this option will follow the physical directory structure of the user-requested directory. |
-e | 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
Although CD is a very common command like ls or echo, the fact that you are reading this article indicates that you are a new Linux user.
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.
Each time you move into a different directory, you can use the pwd command to check the absolute path of the current working directory.
Considering that, let’s move on to the practical.
Go to the Specified Directory
1. Execute the following command to move into the “~/Documents
” directory.
$ cd Documents/
Output:
2. Execute the following command to move into the “/var/www/html
” directory from your current directory.
$ cd /var/www/html
Output:
3. The following command will move you into the parent directory of the current directory (it’s one step above).
$ cd ..
Output:
4. To go back to the previously active directory, use the “–
” dash symbol, as shown.
$ cd -
Output:
5. Enter into another user’s home directory (ex: “jake
“) by specifying the home directory name with the “~
” tilde symbol.
$ cd ~jake
Output:
6. Go to the root directory from the current working directory.
$ cd /
Output:
7. Move into your own home directory by using the following command.
$ cd
#OR
$ cd ~
Output:
8. Enter the directory with a space.
$ cd Documents/'directory with space'/
#OR
$ cd Documents/directory\ with\ space/
Output:
9. Switch to another directory using the current working directory.
$ cd ../proj/scripts/
Output:
10. Move two directories from where you are now.
$ cd ../../
Output:
11. Move into another directory from your current working directory.
$ cd ../Downloads/
Output:
12. Navigate to another directory without specifying its name using “Tab” key.
$ cd ../Doc<TAB>/p<TAB>/s<TAB>/
Output:
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*
Output:
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 “/var/www/html
” path.
$ pushd /var/www/html/
Output:
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.
$ popd
Output:
15. Listing all the content of the directory after moving into it.
$ cd ~/Documents/ && ls
Output:
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.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.