Saturday 17 February 2018

How To: 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.

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:

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:

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:

Linux Tutorials and Materials, LPI Certifications, LPI Guides, LPI Learning

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:

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:

Linux Tutorials and Materials, LPI Certifications, LPI Guides, LPI Learning
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
/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

Related Posts

0 comments:

Post a Comment