Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Working directory

The working directory is the directory from which relative paths are resolved. It is a property of the shell environment and is inherited by subshells and external utilities.

Many commands use the working directory as the default location for file operations. For example, specifying a relative path in a redirection creates the file in the working directory. The ls utility lists files in the working directory if no operand is given.

$ mkdir $$ && cd $$ || exit
$ echo "Hello, world!" > hello.txt
$ ls
hello.txt

Viewing the current working directory

The PWD and OLDPWD variables hold the absolute pathnames of the current and previous working directories, respectively. These variables are updated automatically when the working directory changes. If you modify or unset them manually, automatic updates are no longer guaranteed.

$ echo "$PWD"
/home/user

You can also use the pwd built-in to print the current working directory.

$ pwd
/home/user

Changing the working directory

Use the cd built-in to change the working directory.

$ cd /tmp
$ pwd
/tmp
$ cd /dev
$ echo "$PWD" "$OLDPWD"
/dev /tmp