The cd command is a Linux command meaning “Change Directory”. The function of this command is to change the your current location in the file system hierarchy. This command is one of the most basic of all the Linux commands. However, It is also one of the most used.
File System Hierarchy
It is important to understand the basic concept of the file system hierarchy in order to move around it efficiently. The file system has a root, and from the root branches out to other directories.
Here is a very basic example of a file system hierarchy beginning with the root ( / ).
/ ├── boot ├── dev ├── etc ├── home | ├── mcherisi | └── savona | ├── Desktop | ├── Documents | ├── Downloads | ├── Music | ├── Pictures | | └── linux.jpg | └── Vidoes └── var
When we say move up, or back, that means to step up one branch in the file system hierarchy. For example from savona to home.
Using the cd Command
The cd command is very easy to use. You just have to invoke it along with the directory you want to enter.
In Linux the default location of the prompt in the terminal is the home directory of the active user. You can check this by using the pwd (print working directory) command.
[[email protected] ~]$ pwd /home/savona
So if you want to move to a directory named “example”, you can use the following command:
[[email protected] ~]$ cd example
The cd command does not generate any output. So use the pwd (print working directory) command to see the current full path of your location in the file system.
[[email protected] ~/example]$ pwd /home/savona/example
Change to a Sub-Directory
Of course, it is possible to directly access any sub-directory if you specify its full path. It is not necessary to make multiple directory changes. For example, you can directly access the /var/cache folder from your home directory.
[[email protected] ~]$ cd /var/cache [[email protected]:/var/cache]$ pwd /var/cache
Return To Your Home Folder
If you want to return to the home folder, you can do it by simply typing cd without any argument.
[[email protected] ~/example]$ cd [[email protected] ~]$ pwd /home/savona
You can get the same result if you add the tilde ( ~ ) meta-character to the cd command.
[[email protected] ~]$ cd ~ [[email protected] ~]$ pwd /home/savona
Go Up (Back) One Directory
When working with sub-directories on many occasions, it is necessary to move up one level. To do this, just add “..” to the cd command.
[[email protected]:/var/cache]$ cd .. [[email protected]:/var/]$ pwd /var
Change to Root Directory
You can easily jump to the root directory of the file system by using the forward slash ( / ) with the cd command. The forward slash denotes the root of the file system hierarchy.
[[email protected] tmp]$ cd / [[email protected] /]$ pwd /
Jump Back to Previous Directory
The Linux cd command provides and easy way to go back to a directory you have previously been in.
First, go to the /var/cache directory.
[[email protected] ~]$ cd /var/cache [[email protected]:/var/cache]$
After that, navigate to another directory, for example /etc.
[[email protected]:/var/cache]$ cd /etc [[email protected]:/etc]$
If you type “-” as an argument to the cd command, it will take you to the previous working directory, in this case /var/cache.
[[email protected]:/etc]$ cd - /var/cache
The dash ( – ) is an alias of sorts for the OLDPWD variable that is set by the cd command. This variable constantly updates with the last directory you were in.
[[email protected] etc]$ echo $OLDPWD /var/cache
Access a Folder That Has a Space in The Name
Until now, all of our examples have been directories that have names without blank space. To access directories with spaces in the name you have to escape the spaces or wrap the name in quotes.
Here is an example of how to access a directory that has a blank space in the name using quotes.
[[email protected] ~]$ cd “example folder” [[email protected] ~/ example folder]$ pwd /home/savona/example folder
Here is an example of how to access a directory by escaping the space (or special character).
[[email protected] ~]$ cd example\ folder [[email protected] example folder]$ pwd /home/savona/example folder
To learn more about working with directories and files that contain spaces or special characters read “Working with Filenames that Contain Spaces or Special Characters in Linux“.
Navigating the file system with the cd command is an essential skill if you are going to work on the command line. Using the cd command doesn’t require much practice, but understanding the file system hierarchy does. This is a good place to start in order to become more efficient on the Linux command line.
Resources and Links
This site uses Akismet to reduce spam. Learn how your comment data is processed.