Linux and Unix cd command

Quick links

About cd
Syntax
Examples
Related commands
Related Q&A
Linux and Unix main page

About cd

The cd command, which stands for "change directory", changes the shell's current working directory.

Description

The cd command is one of the commands you will use the most at the command line in linux. It allows you to change your present working directory. You use it to move around within the hierarchy of your file system.

About Directories

In order to help you organize your files, your file system contains special files called directories. Think of them like folders in a file cabinet: they have names, just like files, but their function is to "contain" other files, and other directories. In this way, you can keep the files on your system separate and sorted according to their function or purpose.

All files and directories on your system stem from one main directory: the root directory. There are no directories "above" the root directory; all other directories are "below" the root directory.

Any directory which is contained inside another directory is called a subdirectory. Subdirectories "branch" off the "root" of the directory "tree." Unlike a real tree, directory trees are upside-down: the root is at the top and the branches reach down. When you move into a subdirectory, you are moving "down" the tree; when you move into a directory's parent directory, you are moving "up" the tree.

All directories on your file system are subdirectories of the root directory.

Note: By default, when you open a terminal and begin using the command line, you are placed in your home directory.

How Directories Are Represented

Directories are separated by a forward slash ("/"). For instance, the directory name "documents/work/accounting" means "the directory named accounting, which is in the directory named work, which is in the directory named documents which is in the current directory."

To change into this directory, and make it our present working directory, we would use the command:

cd documents/work/accounting

If the first character of a directory name is a slash, that denotes that the directory path begins in the root directory. So, in contrast to the example above, the directory name "/documents/work/accounting" (note the beginning slash) means "the directory named accounting, which is in the directory named work, which is in the directory named documents which is in the root directory."

To change into this directory, making it our present working directory, we would use the command:

cd /documents/work/accounting

Representing The Root Directory

The root directory itself is represented by a single slash ("/").

To change into the root directory, making it our present working directory, we would use the command:

cd /

Representing The Working Directory

The current directory, regardless of which directory it is, is represented by a single dot (".").

So, running this command:

cd .

...would change us into the directory we're already in. In other words, it would do nothing, but it would do it successfully.

What's actually happening is the dot represents the "assumed" directory; it's a placeholder, and you can use the dot anywhere in a directory name. So, the command:

cd documents

...is the same as the command:

cd ./documents

...and also the same as:

cd documents/.

...as well as:

cd ./documents/.

In all of these examples, the dot represents "the directory assumed to be there". You can use it as a placeholder anywhere you want to tell the shell that a directory goes in that place, and to assume the appropriate value.

Representing The Parent Directory

The parent directory of the current directory — in other words, the directory one level up from the current directory, which contains the directory we're in now — is represented by two dots ("..").

So, If we were in the directory /home/username/documents, and we executed the command:

cd ..

...we would be placed in the directory /home/username.

The double-dot ("..") directory notation can be used anywhere in a directory name to represent going up one level. For instance, if we have two directories, /home/username/documents and /home/username/downloads, and we are currently in /home/username/documents, we could type the following:

cd ../downloads

...and we would be placed in /home/username/downloads.

Another "useless" command, but one that illustrates the way you can use the double-dot notation, is this one:

cd documents/..

...which will place us in the directory one level above the subdirectory documents — in other words, the current directory. (Note that this will only work if the subdirectory documents already exists.)

Similarly, the command:

cd documents/../documents

...is functionally the same as this command:

cd documents

Representing Your Home Directory

Your home directory is the directory you're placed in, by default, when you open a new terminal session. It's the directory that holds all your settings, your mail, your default documents and downloads folder, and many other personal items. It has a special representation: a tilde ("~").

So, if our username is username, and our home directory is /home/username, the command:

cd ~

...is functionally the same as the command:

cd /home/username

...and we can always access the subdirectories of our home directory by placing the tilde as the first component of the the directory name. For instance, if your documents folder is named /home/username/documents, you can always move into that directory using the command:

cd ~/documents

Using A Trailing Slash

Using a slash at the end of a directory name is optional. Directories are simply files, so you don't need to put it there; but if you do put it there, the system knows for sure you're expecting that file to be a directory. For example, if there is a subdirectory off the current directory named awesome, the command:

cd awesome

...is the same as the command:

cd awesome/

However, it's good to know that both are valid.

Command Syntax

cd [-L|-P] directory

Options

-L Force symbolic links to be followed. In other words, if you tell cd to move into a "directory" which is actually a symbolic link to a directory, it moves into the directory the symbolic link points to.

This is the default behavior of cd; normally, it will always act as if -L has been specified.
-P Use the physical directory structure without following symbolic links. In other words, only change into the specified directory if it actually exists as named; symbolic links will not be followed. This is the opposite of the -L option, and if they are both specified, this option will be ignored.
-e If the -P option is specified, and the current working directory cannot be determined, this option tells cd to exit with an error. If -P is not specified along with this option, this option has no function.

Examples

cd hope

The above example would change the working directory to the hope subdirectory if it exists.

cd ../computerhope

The above example would traverse up one level to the parent directory and then down into the directory computerhope.

cd ../../

Traverse two directories up the directory tree. In other words, move into the directory which contains the directory which contains the current working directory.

Related commands

pwd — returns the name of the current working directory
ln — create symbolic links to files or directories
mkdir — make a new directory
rmdir — remove an empty directory

Related Q&A