How to Change Directory in Linux

The 'cd' command moves around the filesystem from the shell

Tux the penguin is the official Linux mascot.

John Coulter / Getty Images

The Linux cd command changes the current directory. It's used when you're in a shell session (for example, when you've launched a terminal window from a graphical desktop) to change the current working directory.

The Linux desktop with two different directory listings.
Lifewire 

Working Directories in Linux

When you use Linux in a graphical environment, you rarely think about the current working directory. However, when you're working in a shell, the system assumes that your scope of work is the current directory unless you tell it otherwise. For example, when you save a file in LibreOffice Writer, it'll default to your home directory, but when you save a file using a text editor in the shell, it'll default to the current working directory.

To check which directory you're in, use the pwd command. It'll print your working directory (hence the command name) to standard output.

How to Use the 'CD' Command to Change the Current Working Directory

The cd command is elegant in its simplicity:

The command only supports two options, both of which are rare:

  • -P: Use the physical directory structure and ignore symbolic links
  • -L: Require that symbolic links are followed

Local and Absolute Pathnames in the 'CD' Command

Use either absolute pathnames or local pathnames with the cd command. An absolute pathname starts at the root directory whereas a local pathname is relative to the current working directory.

For example, if you're currently in /var/www/html but you want to move to /var/www/html/img, either command works:

cd /var/www/html/img
cd img

Shortcuts for Moving Around the Filesystem

Shell prompt with current directories listed.
Screenshot 

Use the following shortcuts with the cd command:

  • /: Moves to the root directory (forward slash)
  • ..: Moves to the parent directory (two periods without a space)
  • ~: Moves to the logged-in user's home directory (tilde)
  • -: Moves to the previous directory (hyphen)

Depending on your shell, you may see either one of the shortcut symbols appended to your username at the shell prompt or the current directory name.

Permissions Problems

In Linux, both directories and files incur permissions. Generally speaking, any file or directory may be readable, writable, or executable to you, depending on whether you're the owner of the object, a part of a group given access to it, or a globally authorized user.

If you try to change to a directory where you cannot normally read the contents, you'll get an error. For example, a regular user account operating in a Bash shell, trying to access /root, will obtain the following error:

Either move using elevated privileges (e.g., through the sudo command) or change the permissions of the directory using the chmod command.