A shell is always "in" some directory, called the current working directory. Commands that take file names start looking there unless you tell them otherwise. The pwd command prints it.
The directory tree
Linux arranges all files in one tree. The top is the root directory, written /. Everything else hangs below it, for example /etc, /usr and /home. Each user normally has a home directory such as /home/pete. The root user's home is usually /root.
Running pwd
$ pwd
/home/peteThe output is an absolute path: it starts with / and lists every directory from the root down to where you are. Reading /home/pete from left to right means: start at the root, go into home, then into pete.
A relative path does not start with /. It is resolved from your current directory. If you are in /home/pete, then Documents/todo.txt means /home/pete/Documents/todo.txt.
Why check where you are?
The same command can affect different files depending on your location. Running pwd before you create, move or delete something is a cheap way to avoid mistakes. Your prompt often shows the directory too, but it may shorten it, for example using ~ for your home directory.
Symbolic links: -L and -P
A symbolic link is a file that points to another location. Suppose /home/pete/site is a link to /var/www. After moving into it, pwd can answer in two ways:
$ cd /home/pete/site
$ pwd
/home/pete/site
$ pwd -P
/var/www-L (logical) keeps the link name you used. -P (physical) resolves links and shows the real directory. In bash, the built-in pwd behaves like -L unless you change its settings.
Most shells provide pwd as a built-in, and there is also a separate /bin/pwd program. Their defaults can differ: GNU /bin/pwd uses -P unless told otherwise. Pass the option you want when it matters.
The shell navigation lab lets you try this on a live system.
Try it
- Open a terminal and run
pwd. - Write down each directory in the path, starting from
/. - Run
echo $HOMEand compare it with thepwdoutput. - Run
pwd -Pand see whether the answer changes.
Check your understanding
pwd (Print Working Directory)
All 4 sections ยท no timer
Unlimited retries
Questions from every section are combined into one assessment. Answer them all correctly to complete the article.
Your completion saves in this browser. Sign in to sync it to your account.