Cd command

From Linux Bash Shell Scripting Tutorial Wiki
Jump to navigation Jump to search

We use the cd command to change the current directory under Linux and Unix operating systems. It is the same as CD or CHDIR command in MS-DOS/Windows operating system. This wiki page explains how to use the cd command to navigate your system’s directory tree.

Purpose

Change the shell working directory or the current directory. The default directory is the value of the HOME shell variable. Use the echo command or printf command to show the default value:

echo "$HOME"

Syntax

Cd command's syntax is as follows on Linux or Unix like operating systems:

cd directory
cd /path/to/new/directory
cd ..
cd
cd [options] [directory]
cd

Please note that the items in square brackets are optional. So when we use the cd command without specifying any directory name, cd returns the user to the home directory set by the HOME shell variable.

Examples

The most command usage is to change the current directory. For example, change the current directory to /etc/ by providing an absolute pathname:

cd /etc/

Verify the current directory path using the pwd command:

pwd

Say inside /etc/ you have another directory called nginx. So instead of typing full absolute pathname:

cd /etc/nginx/

We could type a local pathname that is relative to the current directory:

cd nginx
pwd
cd command in Linux with Examples
cd command in Linux with Examples

. and .. directories

The current directory is represented by a single dot on Linux and Unix-like systems, and two consecutive dots represent its parent directory.

Various cd command shortcuts
Directory Description cd command examples
. The current directory cd .
.. The parent directory (the directory that contains it) cd ..
cd ../../.. # move three level up from the current dir
cd ../conf.d/ # Move up from /etc/nginx/modules-enabled to /etc/nginx/conf.d
~ The home directory (go back to users $HOME folder) cd ~
cd $HOME
cd
- The previous directory (go back to previous directory location) cd -

A tilde as the argument

The cd has the ability for any user to return directly to their home directory by simply using a tilde as the argument:

cd ~
##
## just typing 'cd' would do the same 
##
cd
##
## No need to type the following commands to return to the /home/vivek directory 
##
cd /home/vivek
cd $HOME
How do I use the cd command to go back to HOME directory?
How do I use the cd command to go back to HOME directory?

Returns the user to the previous current directory

Another cool feature that allows users to return to the previous directory without typing the full path. For instance, say you are currently in the /etc/nginx directory and then went to /etc/php/7.4/ directory. Instead of typing the following commands:

pwd
cd /etc/php/7.4/
pwd
cd /etc/nginx/
pwd

You would utterly type the following command to toggling between /etc/nginx/ and /etc/php/7.4/ folders:

pwd
cd -
pwd
cd -
The cd (“change directory”) command is used to change the current working directory easily with 'cd -' toggle option
The cd (“change directory”) command is used to change the current working directory easily with 'cd -' toggle option


Dealing with directories with white space in their names

Say the directory you want to Cd into has blank or white spaces in their name. Then you need to either surround the path with single quotes or use the backslash (\) character to escape the space:

cd 'DIR NAME HERE'
cd 'This is a dir'
cd This\ is\ a\ dir/
Directories with while/black space in their names and cd command
Directories with while/black space in their names and cd command

Force symbolic links to be followed

Pass the -L option as follows:

ls -l symlink
cd -L symlink
cd -L /path/to/symlink
pwd

Use the physical directory structure instead of following symbolic links

Use the -P option to use the physical directory structure without following symbolic link on Unix or Linux:

ls -l symlink
cd -P symlink
cd -P /path/to/symlink
pwd
Handle the symbolic link components with cd command
Handle the symbolic link components with cd command

Environment variables

The following environment variables shall affect the execution of cd command:

CD ENVIRONMENT VARIABLES FOR BASH
Variable Description Examples
CDPATH A {colon}-separated list of pathnames that refer to directories. echo "$CDPATH"
export CDPATH=/etc:/efs
cd nginx # typing this will take you to /etc/nginx
cd app1 # typing this will take you to /efs/app1
HOME It is the name of the directory, used when no directory operand is specified. Your HOME directory cd
OLDPWD A pathname of the previous working directory and used by the cd - echo "$OLDPWD"
cd -
cd "$OLDPWD" && pwd
PWD A pathname of the current working directory echo "$PWD"

Controlling cd command behavior using a bash alias

You can define aliases in your ~/.bashrc or ~/.bash_aliases file, which must call by ~/.bashrc file. For example, here are my favorite aliases for the cd command:

## get rid of command not found when typed 'cd..' ##
alias cd..='cd ..'
 
## a quick way to get out of current directory ##
alias ..='cd ..'
alias ...='cd ../../../'
alias ....='cd ../../../../'
alias .....='cd ../../../../'
alias .4='cd ../../../../'
alias .5='cd ../../../../..'

Getting help

Want to read more about cd command provided by your shell? Try running the help command as follows:

help cd

You can also get information using the man command for your shell:

man bash
man ksh
man tcsh

See also