The cd command is one of the most frequently used commands in Linux, as it’s used to change directories (one of the most basic command line operations) and to navigate within the file system. While most users restrict themselves to the basic usage of cd, there are many other features the tool provides. 

In this tutorial, we will discuss the cd command in a bit more detail. Note that all the examples and instructions mentioned here have been tested on Ubuntu 22.04.

1. How to change your current directory?

You can easily change your current working directory by specifying the new directory as an argument to the cd command:

$ cd [directory-name]

Depending upon where you are and which directory you want to switch to, you may have to specify the absolute path to the latter along with its name.

For example, suppose you are in your home directory, and you want to switch to the Desktop directory (which is usually inside the former), then here’s the command you need to run:

$ cd Desktop

<img alt="Linux CD command – cd command bash" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/cd-basic.png63641ca44860a.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="112" loading="lazy" src="data:image/svg xml,” width=”288″>

As you can see in the above screenshot, the first command shows the home directory as the current working directory. Then, the cd command was used to switch to Desktop. The change in directory was verified using the pwd command. To go to the parent directory, use this command:

cd ..

2. How to quickly switch to home directory using cd command

The tool allows you to quickly switch to your home directory, regardless of your current location (or the present working directory). This can be done by using the tilde (~)  sign with the cd command.

$ cd ~

<img alt="switch to home directory using cd command" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/cd-switchingdirectories.png63641ca48b559.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="76" loading="lazy" src="data:image/svg xml,” width=”500″>

As you can see in the above screenshot, the user was in one of the sub-directories of the Downloads directory. But after using the ‘cd ~’ command, the home directory became the current working directory. 

Note that if the $HOME environment variable is set to your home directory path, then merely running the cd command (without any arguments) will also take you to your home directory.

3. How to quickly switch to the previous directory

The tool also allows you to quickly switch to previous working directory. This feature can be accessed by using the cd – command.

$ cd -

For example: Suppose if you are frequently switching between two directories let’s say Desktop and a subdirectory within the Downloads directory.

<img alt="switch to previous working directory" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/cd-switchingtoprevious1.png63641ca4df5eb.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="86" loading="lazy" src="data:image/svg xml,” width=”500″>

Then instead of mentioning the absolute or relative directory paths each time while switching directories, you can use the cd – command which directly takes you to the previously working directory.

<img alt="cd – command on Linux" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/cd-switchingtoprevious2.png63641ca529934.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="83" loading="lazy" src="data:image/svg xml,” width=”500″>

By default, the cd command follows symbolic links. For example, as you can see below in the screenshot, we have a symbolic link named symlink which points to the symbolic directory.

<img alt="How to make cd not follow symbolic links" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/cd-followsymboliclinks1.png63641ca57c0af.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="52" loading="lazy" src="data:image/svg xml,” width=”500″>

Now, when you run the cd command, by default it follows the symbolic link.

<img alt="test CD not follows symbolic links" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/cd-followsymboliclinks2.png63641ca5ba336.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="80" loading="lazy" src="data:image/svg xml,” width=”312″>

But if you want cd to move you to the physical location of the symbolic link, you’ll have to use the -P command line option.

$ cd -P [symbolic link-name]

For example:

$ cd -P symlink

<img alt="results of cd example" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/cd-followsymboliclinks3.png63641ca60d194.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="88" loading="lazy" src="data:image/svg xml,” width=”304″>

As you can see in the above screenshot, with the use of the -P option, we moved to the physical location of symlink.

5. What is CDPATH and how to use it

The CDPATH environment variabe can be used to define base directory for the cd command.

For example, in one of the earlier options, we discussed the ‘cd ~’ command which quickly takes you to the home directory from any location of your system. In the same way, if you want to quickly switch to any other particular directory irrespective of your current location, you can do this by defining a base directory using the CDPATH environment variable.

Suppose you are mainly working in a particular directory (say Desktop), but are frequently switching between multiple directories. You might be using cd .. , cd ~ or absolute path etc to switch directories. While this approach is not wrong per se, you can make the task (of returning to Desktop) easy by using the CDPATH environment variable – in that case, you’ll only have to run ‘cd Desktop’.

For this, you’ll have to set /home/howtoforge as your base directory:

$ export CDPATH =/home/howtoforge

Now, you can easily and quickly switch to directories that are under /home/howtoforge, including Desktop. Here’s an example:

<img alt="What is CDPATH" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/cd-CDPATH.png63641ca624ed7.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="88" loading="lazy" src="data:image/svg xml,” width=”464″>

As you can see in the above screenshot, first command shows that the user is residing in one of the sub-directories of the Downloads directory. But by using a ‘cd Desktop’ command the user directly switches to the standard Desktop directory (which is inside the home directory).

6. How to set a nickname for a path

The tool also allows you to set a nickname for a path, making it easy for you to access it on the command line. This can be done by using cdable_vars variable. If this variable is set, an argument to the cd command that is not a directory is assumed to be the name of a variable whose value is the directory to change to.

To use this feature, first of all we have to set ‘cdable_vars’ by running the following command on the terminal:

$ shopt -s cdable_vars

To check whether the cdable_vars is set or not, run the following command:

$ shopt

Now, you can define a variable that contains the path for which you want to set a nickname

$ [variable-name] = [path-to-destination-directory]

Here is a working example:

$ htf= /home/howtoforge/Downloads/screencasting-tools

<img alt="How to set a nickname for a path" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/cd-howtosetnickname.png63641ca65b408.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="97" loading="lazy" src="data:image/svg xml,” width=”500″>

Suppose you are working on a project and that project has subdirectories on each level and you frequently need to switch between those directories again and again. Now, switching back to base directory from sub-directory locations, again and again, can be a little time-consuming. This is one of the situations where ‘cdable_vars’ comes to the rescue.

7. How to create a directory and switch to it using a single command

If you want, you can create a directory and also move to that directory by using a single command. To do this, run the following command:

$ mkdir [dir-name] && cd $_

For example:

$ mkdir howtoforge && cd $_

<img alt="How to create directory and switch to it using single command" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/cd-createafile.png63641ca6a026a.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="104" loading="lazy" src="data:image/svg xml,” width=”336″>

As you might already know, the && operator is used for executing multiple commands, and $_ expands to the last argument of the previous command.

8. How to create a directory named ‘-’ and switch to it

As we have already discussed earlier in this tutorial, the ‘-’  symbol when used with the cd command takes you to the previous working directory. But what if you want to switch to a directory with ‘-‘ as its name?. Don’t worry you can do this by using the relative path:

$ cd ./-

<img alt="How to create a directory named ‘-’ and switch to it" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/cd-howtocrearedirectory-andentertoit1.png63641ca6e1dc1.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="144" loading="lazy" src="data:image/svg xml,” width=”248″>

Conclusion

In this tutorial, we’ve tried to cover most of the features the cd command offers. So, if you try out all the examples we’ve explained here, you’ll get a good idea about the tool. For other features and details, head to the tool’s man page (or run ‘info cd’ or ‘help cd’ commands).