HowTo: Use pwd Command In Linux / UNIX

byVivek GiteonMarch 9, 2013

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:

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.

Syntax

The syntax is:

pwdpwd[options]var=$(pwd)echo"The current working directory $var."

Examples

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

/home/vivek

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:

/ - Forward slash - The root directory on your system or the file system.

A typical Linux/Unix shell session with pwd

## Where am I?pwd## List the contents of the current directorylsls -l
# Change the current directory to Videoscd Videos
pwd

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:pwd To use the binary version, type full path /bin/pwd:/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:

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

/home/vivek/realdata/scripts/utils

/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:

#!/bin/bash## Get the working dir_d="$(pwd)"## cd to targetcd /nas03/nixcraft/images/today
## do somethingecho"Uploading data to cdn..."## get back to old dircd"$_d"

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

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