cd command in Linux with examples

One of the most essential commands in Linux is the change directory command or cd command. This is how you will navigate to different directories throughout your system. Using this command will change your current working directory to a different one you specify.

The cd command has few additional functionalities as well. You’ll find a few of these options very helpful as they cover a wide array of administration situations; running the cd command to change directories is one of the most common things you’ll do in your Linux terminal daily.

In this tutorial, you’ll learn how to use the cd command in Linux through examples. Follow along below to learn about the various functionalities that you can utilize of this command.

In this tutorial you will learn:

  • How to use the cd command on Linux
cd command in Linux with examples
cd command in Linux with examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software cd
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options

The cd command allows the user to change the directory in which a user is working at a given time. We’ll refer to this as the “present or current working directory” as we did in the introduction of this tutorial. The other functionalities of cd are mostly just changing to certain directories you frequently need to work in. This allows for better efficiency and convenience. In the examples below, we’ll show you exactly how to utilize these functionalities to make the most of the cd command.

cd command in Linux Basic and Advanced Examples

  1. Running the cd command followed by the directory will just change to the specified directory. This change will happen from within your present working directory (relative path) or you can specify an absolute path.
    $ cd Downloads
    

    Running the cd command followed by a directory name will change to that directory
    Running the cd command followed by a directory name will change to that directory
  2. We can use cd / to change to the root directory. The root directory is the directory at the top of your filesystem hierarchy.
    $ cd /
    
    Using cd / to change to the root directory
    Using cd / to change to the root directory

    As you can see in the screenshot above, the output from cd / changes the present working directory to the root directory.

  3. In the example above, we’ve shown how the cd command can be used to to navigate to root directory. But once you’re in the root directory, you might find it tedious to run the cd command over and over again just to get back to your home directory. But as you’ll recall from the introduction of this post, the cd command can change to certain directories you need to work in and out of for more efficiency and convenience. For example, let’s change to the home/user/Downloads. We can achieve this by typing the absolute file path following the cd command.
    $ cd /home/user/Downloads
    
    Using cd and the absolute path from the root directory to the /home/user/Downloads directory
    Using cd and the absolute path from the root directory to the /home/user/Downloads directory

    As you can see in the screenshot above, changing to specific directories from anywhere in the system can be achieved by typing the absolute file path after the command.

  4. Another way you can use the cd command is by using cd .. to change to the previous directory. This command is essential for system administrating and general terminal use. So, committing this to memory will benefit you greatly if you’re using a Linux system in any capacity.
    $ cd ..
    

    You can go back multiple directories at once by listing more double dots. This next example would go backwards two directories.

    $ cd ../..
    
    Using cd .. to change to the previous directory
    Using cd .. to change to the previous directory

    As you can see in the screenshot above, the cd .. command allows us to instantly and efficiently change to one directory up. Obviously this command will not do anything if you are already in the root / directory.

  5. The cd ~ command can be used to change to the current user’s home directory. Changing to the home directory without this command is a lot less efficient, as the entire file path would have to be specified. We’ll show you an example of how to use its syntax.
    $ cd ~
    

    Another way to do the same thing is by just entering cd all by itself.

    $ cd
    

    Using two different cd commands to change to the home directory
    Using two different cd commands to change to the home directory
  6. To go back to the previous directory where you last were, you can use the - character.
    $ cd -
    
NOTE
You can always use the man command to read more about the cd command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Closing Thoughts

In this guide, we learned all about the cd command on Linux. The cd command is important to memorize to use the Linux terminal as efficiently as possible when navigating the system’s directories. It’s especially useful for its cd .. functionality, as it allows you to swiftly change to one or more directories backwards without having to type out the entire file path.