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:
Now check with which command what is the path of cd command binary(if any):
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.
If you try to access the manual entry for any internal command, there is no separate manual page for them.
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):
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.
The tilde sign (~) also represents the home directory. You can se this as well to go to your home directory.
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.
The . directory represents the current directory and the .. represents the parent directory. To go to parent directory, use ..
The . directory will not change your PWD in most of the cases. For example:
But if your current directory is renamed somehow, then your PWD will be changed with this command:
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:
Changing PWD with 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.
The last working directory is strored in OLDPWD variable. If you try to use previous command in a new terminal, it shows following error:
You can use some bash tricks as well with cd command.
With ? wild card: