Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Linux and Unix pwd command tutorial with examples

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 built-in 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

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 built-in

In most shells pwd is a shell built-in. 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 built-in command

Whilst most shells have pwd as a shell built-in 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 built-in 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


This post first appeared on Home | Shape Shed, please read the originial post: here

Share the post

Linux and Unix pwd command tutorial with examples

×

Subscribe to Home | Shape Shed

Get updates delivered right to your inbox!

Thank you for your subscription

×