Linux cd Command Tutorial for Beginners (8 Examples)

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

Linux CD command - cd command bash

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 ~

switch to home directory using cd command

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.

switch to previous working directory

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.

cd - command on Linux

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.

How to make cd not follow symbolic links

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

test CD not follows symbolic links

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

results of cd example

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:

What is CDPATH

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

How to set a nickname for a path

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 $_

How to create directory and switch to it using single command

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

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


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

Share this page:

3 Comment(s)

Add comment

Please register in our forum first to comment.


By: Pete at: 2017-05-08 18:24:30

cd  with no argument is the same as 'cd ~' in all shells I've used.  Been that way for 20+ yrs.  Save 2 characters, use 'cd<enter>'

A little coverage of cdpath would be nice too. It is very helpful as a character saving technique.

By: nonya at: 2017-05-08 20:49:21

cd .. takes you to home directory. Many of these were the same in DOS 3.3 and up.

By: Petar at: 2017-05-28 02:17:12

Just to add some basic use to the good examples before:

cd /    - Switch to root directory

cd ..   - Switch to one step prevous directory in the tree

cd some* - Switch to directory that starts with "some" and anything else afterwards