Understanding Linux cd Command with Examples

The cd command is the most basic command in *nix world. What linux cd command does is it changes your current working directory. This article details about this command, what this command can do and the internals about the command.

cd command: An internal command

The BASH shell is the default shell in most Linux distributions. The shell has some internal commands of its own. cd is one od the internal commands. I will explain what internal commands are and why cd is an internal command. First, confirm that your current shell with SHELL environment variable:

The current Shell

Now check with which command what is the path of cd command binary(if any):

Locating binary for cd

It results in no output. Its because no binary is present on the system for cd command. Still you are able to execute the command. This is because cd is an BASH internal command. Internal commands are built into the shell. Another internal command 'type' will give you the information that cd is internal command.

Type command on cd

If you try to access the manual entry for any internal command, there is no separate manual page for them.

Manual page for cd

For these internal commands, no separate process is created. So they are very fast.

To get full list of internal commands, you can give help command (which itself is an internal command):

Help command output - 1

Help command output - 2

Why cd is internal command?

To keep things simple, I won't go into much details, but it requires a little understanding of Unix processes to understand the answer to this question.
Whenever a processes is created by BASH, it is executed in a subshell of BASH (child process of current BASH process). The new processes makes changes and outputs (if reqquired) and no property of this subshell is returned to the parent when the process dies. Note that cd command changed the PWD of the shell. If cd were an external command, it would have changed the PWD of the subshell, returning nothing to the parent shell. And hence, the PWD of the shell will never change. All the commands that make changes to the environment of the shell, must be implemented as internal command. we could never achieve what we require by making cd an external command.

Now let us explore cd command usage.

cd command usage:

If you issue cd command without any argument, it will take you to your home directory, no matter what your current directory is.

cd into home directory

The tilde sign (~) also represents the home directory. You can se this as well to go to your home directory.

cd into home directory using tilde

If you are root, you can go to any user's home directory by using tilde sign followed by that user's name. In some Linux distributions, unpriviledged users do not by default have permission to go to other users' home directories.

cd into other user's home

The . directory represents the current directory and the .. represents the parent directory. To go to parent directory, use ..

parent directory

The . directory will not change your PWD in most of the cases. For example:

cd to . directory

But if your current directory is renamed somehow, then your PWD will be changed with this command:

cd to . renamed

In BASH and most of other shells, you can provide two types of paths: Absolute path and Relative path. Absolute paths begin with / and are independent of your PWD. On the other hand, relative paths never begin with / and depend what your PWD is.

Changing PWD with absolute path:

cd using absolute path

Changing PWD with relative path:

cd using relative path

Toggling between two directories:

The "cd -" command will take you to the last working directory. You can toggle between two directories with this command.

toggle between two directories

The last working directory is strored in OLDPWD variable. If you try to use previous command in a new terminal, it shows following error:

cannot change to last directory

You can use some bash tricks as well with cd command.
With ? wild card:

cd using ? wild card

Using *

cd using * wild card

About Raghu

Raghu is working as Linux Server Administrator in Acknown Technologies Pvt. Ltd. He has been using Linux from last 5 years. He completed his RHCE certification in 2009. He likes to read about Linux and other Open Source Technologies and write articles on these.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.