Linux and Unix pwd command tutorial with examples
Tutorial on using pwd, a UNIX and Linux command for printing the name of the current working directory. Examples of printing the current working directory, avoiding symlinks and how to get the current working directory in shell scripts.
What is the pwd command in UNIX? ¶
The pwd
command is a command line utility for printing the current working
directory. It will print the full system path of the current working directory
to standard output. By default the pwd
command ignores symlinks, although the
full physical path of a current directory can be shown with an option. The pwd
command is normally a shell builtin meaning it is part of the code that runs the
shell rather than an external executable.
How to print the current working directory ¶
To print the current working directory run the pwd
command. The full path of
the current working directory will be printed to standard output.
pwd
/home/george
How to avoid symlinks ¶
To avoid symlinks pass the -P
option. This will cause pwd
to show the
physical location rather than a symlink. To demonstrate this the following
example shows a folder that is symlinked to another location. Without the -P
option the symlink is ignored. In the following example the folder zsh
is a
symlink.
ls -la | grep -E '.zsh$'
lrwxrwxrwx 1 george users 25 Sep 15 2013 .zsh -> /home/george/dotfiles/zsh
Running pwd
within the zsh
folder does not show the symlink.
cd .zsh
pwd
/home/george/.zsh
Running pwd
with the -P
shows the physical location.
cd .zsh
pwd -P
/home/george/dotfiles/zsh
pwd is normally a shell builtin ¶
In most shells pwd
is a shell builtin. This means the command is present
in the shell rather than calling an external program. This means that the code
will run significantly faster than calling an external executable.
which pwd
pwd: shell builtin command
Whilst most shells have pwd
as a shell builtin the command also exists on
systems as an executable. On my own system the executable is located at
/bin/pwd
.
/bin/pwd
/home/george/dotfiles/zsh
How to reference pwd in shell scripts ¶
In most shells the $PWD
variable is available and is set each time a user or
in script changes directory. As such this variable can be referenced to show the
current working directory.
echo $PWD
/home/george
Although $PWD
is ubiquitous it is specific to the shell running the command so
some prefer to reference the pwd
command and store it in a variable. This
makes scripts more portable as they will call the system executable if the shell
builtin or the $PWD
variable does not exist.
CWD=$(pwd)
echo $CWD
/home/george
Althought the $PWD
variable is present is almost all shells it is possible it
may not be there. If you are really paranoid use CWD=$(pwd)
but in most cases
$PWD
will work.
Further reading ¶
- pwd man page
- pwd Wikipedia page
- Is it better to use $(pwd) or $PWD
- HowTo: Use pwd Command In Linux / UNIX
Tags
Can you help make this article better? You can edit it here and send me a pull request.
See Also
-
Configuring and working with Cloudfront Logs
Example of how to setup Cloudfront to log to S3, enable log rotation and how to download and work with combined Cloudfront log files. -
Linux and Unix df command tutorial with examples
Tutorial on using df, a UNIX and Linux command for reporting file system disk space usage. Examples of viewing free disk space, viewing in human readable format, showing filesystem types and including and excluding specific filesystem types. -
Linux and Unix wget command tutorial with examples
Tutorial on using wget, a Linux and UNIX command for downloading files from the Internet. Examples of downloading a single file, downloading multiple files, resuming downloads, throttling download speeds and mirroring a remote site.