≡ Menu

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.

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 <http://gnu.org/licenses/gpl.html>.
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 bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
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 < webmaster@cyberciti.biz> 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)

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

Share this tutorial on:

Like this? Follow us on Twitter OR support us by using Patreon

{ 5 comments… add one }
  • Chris F.A. Johnson March 11, 2013, 1:59 am

    Generally, there is no need to use the pwd command.

    In bash (and all POSIX shells) the PWD variable contains the current directory.

  • Abdul kadir November 6, 2013, 7:07 am

    I have tried your scirpt but cd command is not working in shell script.

    I have created the script. (Ubuntu 12.04 LTS)

    sudo vim change
    ## where am i?
    ## List the contents of the current directory
    ls -l
    # Change the current directory to Videos
    cd Videos

    But cd command is not working. Plz help.

    Abdul Kadir

  • Jay February 13, 2014, 10:02 am

    FWIW I think `pwd` is an acromyn for ‘present working directory’

  • Sarah Thompson March 11, 2014, 11:30 am

    guys I neeed help ;) im trying to write a shell script that allows a user to: display a list of current users, display a list of all files including hidden files in the home directory, output a calender for the current month and then quit the script. If anyone helps me it would be much appreciated and btw guys im doing this on raspberry pi ;) ;) x

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , , , , , , , , , , , , ,