How to use the Linux cd command to change directories

The cd command is a particularly useful one that enables you to quickly change from one directory to another. This Linux command has many practical functions.

What is the Linux cd command?

You can use the cd command to switch which directory you’re currently working in in most common Linux distributions, as well as in Ubuntu. Once done, your new target will be the current directory. On Linux cd is short for ‘change directory’. The command is one of the most widely used by most Linux users when working with the shell. The important thing you need to remember is that you need to avoid any typos. If you need to find the directory first, then we’d recommend you use the Linux find command.

How does the cd command work?

While working in the shell, you’ll often need to switch the directory you’re in. This is where the cd command comes in. When entering the command you need a relative path which goes to the hierarchy of the subfolders and from your path to the next path up. On the other hand, you can also enter an absolute path and go directly to your target. You’ll be taken there so long as the directory exists and you have sufficient access rights. If this isn’t the case then you will stay in your current directory and will receive an error message.

What does the cd syntax look like?

This is the general cd command syntax:

$ cd [Option] [Directory]
shell

cd is the command itself and you can use the options to further specify it. [Directory] is the exact name of the location you want to go to.

What options does the cd command have?

You have two options with the cd command:

  • -L: This cd command option will follow the logical directory structure. This is a standard setting.
  • -P: This option, on the other hand, follows the physical directory structure.

Examples of the Linux cd command

So that you can better see how the cd command works, here are a few simple examples. You’ll soon see that this is a useful command that you will use almost daily to easily switch to a different directory.

$ cd ~
shell

This is the command to use to go to your home directory.

$ cd /
shell

Use this option to switch to your root directory.

$ cd /usr/local
shell

Use this to switch to the subdirectory /usr/local.

$ cd -
shell

This option will take you to your previous working directory.

Was this article helpful?
Page top