HowTo: Use pwd Command In Linux / UNIX

How do I use the pwd command in Linux or Unix like operating systems? How can I use pwd command in UNIX or Linux shell scripts for automation purpose?

The pwd is an acronym for print working directory. The pwd command is considered as one of the most frequently used commands on Linux, AIX, HP-UX, *BSD, and other UNIX like operating systems along with the ls, and cd commands. It can be used for the following purposes under Apple OS X or UNIX or Linux operating systems:

=> Find the full path to the current directory.

=> Store the full path to the current directory in the shell variable.

=> Verify the absolute path.

=> Verify the physical path i.e exclude symbolic links.

The current directory

The current directory is nothing but the directory in which you are currently operating while using bash or ksh or zsh or tcsh/csh shell. You need to open a terminal (GUI) or login on a console to use a command line.


The syntax is:

pwd [options]
echo "The current working directory $var."


To print current working directory, enter:
$ pwd
Sample outputs:


In this example, /home/vivek is your current directory. The full path of any directory under Unix like operating systems always stats with a forward slash. In short:

  1. / – Forward slash – The root directory on your system or the file system.
  2. home – Sub-directory
  3. vivek – Sub-directory

To store current directory in a shell variable called x, enter:


To print the current directory either use printf command or echo command:

echo "The current working directory : $x"


printf "The current working directory : %s" $x

A typical Linux/Unix shell session with pwd

Most Unix users use the pwd command along with ls and cd commands:

## Where am I?
## List the contents of the current directory
ls -l
# Change the current directory to Videos
cd Videos

Sample outputs:

Fig.01: A typical shell user session with pwd, ls, and cd commands.

In this above examples, the pwd command is used for confirming that the current directory has actually been changed.

Shell pwd vs /bin/pwd

Your shell may have its own version of pwd, which usually supersedes the version described below. To see all locations containing an executable named pwd, enter:
$ type -a pwd
Sample outputs:

pwd is a shell builtin
pwd is /bin/pwd

By typing pwd, you end up using the shell builtin provided by bash or ksh:
To use the binary version, type full path /bin/pwd:
Please note that both commands print the current/working directory. However, /bin/pwd has few more options as described below.

pwd options

To display the logical current working directory, enter:
$ pwd -L
The -L option cause pwd to use $PWD from environment, even if it contains symlinks. If the contents of the environment variable PWD provide an absolute name of the current directory with no . or .. components, but possibly with symbolic links, then output those contents. Otherwise, fall back to default -P handling:
$ pwd -P
Display the physical current working directory (all symbolic links resolved). For example, ~/bin/ is symbolic link:
$ pwd
$ ls -l ~/bin/

Sample outputs:

lrwxrwxrwx 1 vivek vivek 35 May 13  2012 /home/vivek/bin -> /home/vivek/realdata/scripts/utils/

cd to ~/bin/ and verify the current working directory with pwd:
$ cd ~/bin/
$ pwd

Sample outputs:
To see actual physical current working directory and avoid avoid all symlink called /home/vivek/bin, enter:
$ pwd -P
Sample outputs:


/bin/pwd options

The /bin/pwd version of pwd command has a two more additional options. To display pwd command version, enter:
$ /bin/pwd --version
Sample outputs:

pwd (GNU coreutils) 8.5
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Jim Meyering.

To see information about pwd, enter:
$ /bin/pwd --help
Sample outputs:

Usage: /bin/pwd [OPTION]...
Print the full filename of the current working directory.
  -L, --logical   use PWD from environment, even if it contains symlinks
  -P, --physical  avoid all symlinks
      --help     display this help and exit
      --version  output version information and exit
NOTE: your shell may have its own version of pwd, which usually supersedes
the version described here.  Please refer to your shell's documentation
for details about the options it supports.
Report pwd bugs to
GNU coreutils home page: <>
General help using GNU software: <>
For complete documentation, run: info coreutils 'pwd invocation'

Shell script example

A basic version:

## Get the working dir
## cd to target
cd /nas03/nixcraft/images/today
## do something
echo "Uploading data to cdn..."
## get back to old dir
cd "$_d"

A complete working example that uses the pwd command to inform user about the current working directory before setting up the directory permissions.

# Purpose: Set secure read-only permission for web-server DocumentRoot
# Author: nixCraft <> under GPL v2.x+
# Usage: ./script 
#        ./script /var/www
## Get dir name from command line args
# if $1 not passed, fall back to current directory
## get the current working dir
## Permissions 
# Change me to Apache/Lighttpd/Nginx user:group names
## Die if _dir not found
[ ! -d "$_dir" ] && { echo "Directory $_dir not found."; exit 1; }
echo "Chaning file permissions for webserver directory and files to restrctive read-only mode for "$_dir""
read -p "Your current working directory is ${_pwd}. Are you sure (y / n) ?" ans
if [ "$ans" == "y" ]
     echo "Working on $_dir, please wait..."
     chown -R ${_user}:${_group} "$_dir"
     chmod -R $_fp "$_dir"
     find "$_dir" -type d -print0  | xargs -0 -I {} chmod $_dp "{}"

Sample outputs:

Fig.02: Sample shell script session (click to enlarge)

A note about the bash/ksh working directory shell variables

The bash and ksh (and other shells) set the following environment variable while using the cd command:

  1. OLDPWD – The previous working directory as set by the cd command.
  2. PWD – The current working directory as set by the cd command.

To print environment variable, enter:
$ echo "$PWD $OLDPWD"
To use environment variable, enter:
$ pwd
$ cd /home/sales/pdfs
$ ls
$ vi sample.broacher.txt
# go back to /home/accounts/office/b/bom/f/2/2008/10/
$ cd "$OLDPWD"
$ pwd

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


Start your Bitcoin Exchange with our Software

Start your Bitcoin Exchange with our Software Setup your white label bitcoin exchange right away. Or create your own cryptocurrency using...

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....