You are on page 1of 6

7/5/2019 Linux cd command tutorial for beginners (8 Examples)

Linux cd command tutorial for beginners (8


Examples)

The cd command is one of the On this page


most frequently used commands
in Linux, as it's used to change 1. How to change your current directory?
directories (one of the most 2. How to quickly switch to home directory using
basic command line operations). cd command
While most users restrict 3. How to quickly switch to previous working
themselves to the basic usage directory
of cd, there are many other 4. How to make cd not follow symbolic links
features the tool provides. 5. What is CDPATH and how to use it
6. How to set a nickname for a path
7. How to create directory and switch to it using
In this tutorial, we will discuss single command
the cd command in a bit detail. 8. How to create a directory named ‘-’ and switch
Note that all the examples and to it
instructions mentioned here have Conclusion
been tested on Ubuntu 16.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 cdcommand:

$ 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

https://www.howtoforge.com/linux-cd-command/ 1/6
7/5/2019 Linux cd command tutorial for beginners (8 Examples)

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.

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 ~

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 previous working 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.

https://www.howtoforge.com/linux-cd-command/ 2/6
7/5/2019 Linux cd command tutorial for beginners (8 Examples)

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.

4. How to make cd not follow symbolic links

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.

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

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

https://www.howtoforge.com/linux-cd-command/ 3/6
7/5/2019 Linux cd command tutorial for beginners (8 Examples)

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:

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
https://www.howtoforge.com/linux-cd-command/ 4/6
7/5/2019 Linux cd command tutorial for beginners (8 Examples)

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

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 rescue.

7. How to create directory and switch to it using 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:

https://www.howtoforge.com/linux-cd-command/ 5/6
7/5/2019 Linux cd command tutorial for beginners (8 Examples)

$ mkdir howtoforge && cd $_

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 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 relative path:

$ cd ./-

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).

https://www.howtoforge.com/linux-cd-command/ 6/6

You might also like