CD Command

The cd (change directory) command is used to change the current working directory in Linux and other Unix-like operating systems. The current working directory is the directory in which the user is currently working in. The syntax for the cd command is as follows:

cd [OPTIONS] directory

Absolute path starts from the system root /, and relative path starts from your current directory. By default, when you log into your Linux system, your current working directory is set to your home directory. Assuming that Documents directory exists in your home directory, to navigate to the this directory type below command

cd Documents

To navigate to the same directory by using its absolute path, use below command

cd /home/username/Downloads

where username is user name of logged in user.

Current working directory is represented by a single dot (.), and parent directory by two dots (..). Suppose you are currently in the /home/username/Downloads directory, to switch to the /home directory (move two levels up) run the following:

cd ../../

Here is another example. Let’s say you are in /home/username/Downloads directory, to switch to the /home/username/Documents directory (move two levels up) run the following on terminal

cd ../Documents

To change back to the previous working directory, pass the dash (-) character as an argument to the cd command:

cd -

To navigate to your home directory type cd or to use the tilde (~) character, as shown below:

cd ~

If directory name has spaces in its name, surround the path with quotes or use the backslash (\) character to escape the space:

cd 'name \ with \ space'

To show last working directory from where we moved (use ‘–‘ switch) as shown.

cd --

PWD Command

The pwd (Print Working Directory) command prints the current working directory. It prints the current directory name with the complete path starting from root (/). Syntax of pwd is

pwd [OPTION]

You can also use the environment variable $PWD. For example

echo $PWD
/home/username

To save the output of pwd (or other) command in a variable is to enclose the command in $() or “ (backticks):

var=$(command)
var=`command`