Example uses of the command "cd"

Navigating Linux
Comstock/Stockbyte/Getty Images

Introduction

In this guide you will find out how to navigate around your file system using the Linux "cd" command.

Your Linux system is laid out in a tree format with the / directory at the very top and various other directories underneath.

Common folders under the / directory are as follows:

  • bin
  • boot
  • cdrom
  • dev
  • etc
  • home
  • lib
  • lib64
  • lost+found
  • media
  • mnt
  • opt
  • proc
  • root
  • run
  • sbin
  • srv
  • sys
  • tmp
  • usr
  • var

You can find out what all these folders are used for by reading this guide showing 10 essential command for navigating the file system using Linux.

Basic Navigation Using The cd Command

Imagine you have the following folder setup under your home folder:

  • Home
    • Username
      • Pictures
        • Family Photos
        • Holiday Photos
        • Christmas Photos
      • Downloads
      • Videos
        • Christmas 2015
        • Christmas 2014
      • Music
        • Reggae
        • Jazz
        • Rock

When you open a terminal window you will generally find yourself in your home folder. You can confirm this using the pwd command.

pwd

The results will be something along the lines of /home/username.

You can always get back to the /home/username folder by typing the cd tilde command:

cd ~

Imagine you are in the /home/username folder and you want to get to the Christmas Photos folder.

You can do it in many different ways.

For example you can run a series of cd commands as follows:

cd Pictures
cd "Christmas Photos"

The first command would move you down from the username folder down to the Pictures folder. The second command takes you down from the Pictures folder to the Christmas Photos folder.

Note that "Christmas Photos" is in quotes as there is a space in the folder name.

You can also use the backslash instead of the quotes to escape the space in the command. For example:

cd Christmas\ Photos

Instead of using two commands you could have just used the one as follows:

cd Pictures/Christmas\ Photos

If you weren't in the home folder and you were in a much higher level folder such as / you can do one of a number of things.

You could specify the entire path as follows:

cd /home/username/Pictures/Christmas\ Photos

You could also use the tilde to get to the home folder and then run the command as follows:

cd ~
cd Pictures/Christmas\ Photos

The other way is to use the tilde all in one command as follows:

cd ~/Pictures/Christmas\ Photos

What this means is that it doesn't matter where you are in the file system you can get to any folder below the home folder by using the notation ~/ as the first characters in the path.

This helps when trying to get from one low level folder to another. For example imagine you are in the Christmas Photos folder and now you want to go to the Reggae folder which is under the Music folder.

You could do the following:

cd ..
cd ..
cd Music
cd Reggae

The two dots signify that you want to go up a directory. If you want to go up two directories you would use the following syntax:

cd ../..

And three?

cd ../../..

You could have specified the cd command all in one command as follows:

cd ../../Music/Reggae

Whilst this works it is much better to use the following syntax as it saves you having to work out how many levels you need to go up before going down again:

cd ~/Music/Reggae

Symbolic Links

If you have symbolic links it is worth knowing about a couple of switches which define the behaviour of the cd command when following them.

Imagine that I created a symbolic link to the Christmas Photos folder called Christmas_Photos. This would save having to use the backslash when navigating to the Christmas Photos folder. (Renaming the folder would probably be a better idea).

The structure now looks like this:

  • Home
    • Username
      • Pictures
        • Family Photos
        • Holiday Photos
        • Christmas Photos
        • Christmas_Photos
      • Downloads
      • Videos
        • Christmas 2015
        • Christmas 2014
      • Music
        • Reggae
        • Jazz
        • Rock

The Christmas_Photos folder isn't a folder at all. It is a link pointing to the Christmas Photos folder.

If you run the cd command against a symbolic link which points to a folder you will be able to see all the files and folders within that folder.

According to the manual page for CD the default behaviour is to follow symbolic links.

For example look at the command below

cd ~/Pictures/Christmas_Photos

If you run the pwd command after running this command you will get the following result.

/home/username/Pictures/Christmas_Photos

To force this behaviour you can use the following command:

cd -L ~/Pictures/Christmas_Photos

If you want to use the physical path you need to enter the following command:

cd -P ~/Pictures/Christmas_Photos

Now when you run the pwd command you will see the following results:

/home/username/Pictures/Christmas Photos

Summary

For more information click here for the cd manual page.

More From Us