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.
Syntax
The syntax is:
pwd pwd [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.
- home – Sub-directory
- vivek – Sub-directory
To store current directory in a shell variable called x, enter:
x=$(pwd)
To print the current directory either use printf command or echo command:
echo "The current working directory : $x"
OR
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? pwd ## List the contents of the current directory ls ls -l # Change the current directory to Videos cd Videos pwd
Sample outputs:
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:
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:
/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 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.
#!/bin/bash # 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 _dir="${1:-.}" ## get the current working dir _pwd="$(pwd)" ## Permissions _dp="0544" _fp="0444" # Change me to Apache/Lighttpd/Nginx user:group names _user="www-data" _group="www-date" ## 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" ] then 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 "{}" fi
Sample outputs:
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:
- OLDPWD – The previous working directory as set by the cd command.
- 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
/home/accounts/office/b/bom/f/2/2008/10/
$ cd /home/sales/pdfs
$ ls
$ vi sample.broacher.txt
# go back to /home/accounts/office/b/bom/f/2/2008/10/
$ cd "$OLDPWD"
$ pwd
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 5 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Generally, there is no need to use the pwd command.
In bash (and all POSIX shells) the PWD variable contains the current directory.
I have tried your scirpt but cd command is not working in shell script.
I have created the script. (Ubuntu 12.04 LTS)
But cd command is not working. Plz help.
Regards
Abdul Kadir
FWIW I think `pwd` is an acromyn for ‘present working directory’
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
Try posting on our forum @ http://nixcraft.com/