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|
|Host System and Architecture||Ubuntu 22.10 (x64)|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
|Discussed Tools in this Article|
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:
|-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 “|
|[email protected]||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/ ~/Documents$
2. Execute the following command to move into the “/var/www/html” directory from your current directory.
~/Documents$ cd /var/www/html /var/www/html$
3. The following command will move you into the parent directory of the current directory (it’s one step above).
/var/www/html$ cd .. /var/www$
4. To go back to the previously active directory, use the “–” dash symbol, as shown.
/var/www$ cd - /var/www/html /var/www/html$
5. Enter into another user’s home directory (ex: “jake“) by specifying the home directory name with the “~” tilde symbol.
/var/www/html$ cd ~jake /home/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.
/home/jake$ cd $ #OR /home/jake$ cd ~ $
8. Enter the directory with a space.
$ cd Documents/'directory with space'/ ~/Documents/directory with space$ #OR $ cd Documents/directory\ with\ space/ ~/Documents/directory with space$
9. Switch to another directory using the current working directory.
~/Documents/directory with space$ cd ../proj/scripts/ ~/Documents/proj/scripts$
10. Move two directories from where you are now.
~/Documents/proj/scripts$ cd ../../ ~/Documents$
11. Move into another directory from your current working directory.
~/Documents$ cd ../Downloads/ ~/Downloads$
12. Navigate to another directory without specifying its name.
$ cd ../Doc<TAB>/p<TAB>/s<TAB>/ ~/Documents/proj/scripts$
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.
~/Documents/proj/scripts$ cd ~/Dow* ~/Downloads$
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.
~/Downloads$ pushd /var/www/html/ /var/www/html ~/Desktop /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.
/var/www/html$ popd ~/Downloads ~/Downloads$
15. Listing all the content of the directory after moving into it.
~/Downloads$ cd ~/Documents/ && ls 'directory with space' proj ~/Documents$
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.