CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / File & Directory Management / pwd

pwd

Print the current working directory

Beginneressentialnavigationscripting

pwd prints the full path of the directory you are in. It sounds trivial, but knowing the difference between the logical path (-L, with symlink names) and the physical one (-P) matters when deploys switch symlinks or a directory is deleted under you.

Syntaxpwd [-L | -P]

Watch it run

You've jumped around between directories over SSH and want to be sure where the next command will run.

devops@devops-lab: /var/log — zsh — 96×28
➜ /var/log
Step 1 of 4
pwd

pwd prints the absolute path from / of the current directory. Run it before anything destructive like rm or chmod -R.

Anatomy of a command

$
builtincd

Change into the directory computed by the substitution that follows.

How it works

There are two pwds: the shell builtin (what you get when you type pwd) and /usr/bin/pwd from coreutils. The bash builtin defaults to -L and prints $PWD, the logical path bash has tracked as you cd, symlink names included. Builtin pwd -P resolves the symlinks in that remembered string. /usr/bin/pwd asks the kernel instead: the getcwd() system call walks from the process's current directory inode up through the .. entries to / and returns the real path. The kernel keeps a reference to your working directory, not a path, so if someone deletes or renames the directory you are in, the builtin still prints the old $PWD while getcwd() fails or returns a different name. For other processes, /proc/PID/cwd (or pwdx PID) gives the same physical answer.

Options you'll actually use

pwdPrint the logical working directory (bash default: $PWD)
-LLogical: keep symlink names as you typed them
-PPhysical: resolve the symlinks in the path (builtin: in $PWD)
/usr/bin/pwdThe coreutils binary: physical by default, asks the kernel
$PWD / $OLDPWDShell variables holding the current and previous directory
pwdx PIDPrint another process's working directory (procps)

Common mistakes

Assuming pwd shows the real directory when you entered through a symlink

The builtin prints the logical path (-L), which keeps symlink names; even builtin -P only resolves the remembered name, which can be stale after the link moves.

/usr/bin/pwd or readlink /proc/$$/cwd for the kernel's answer
DIR=`pwd`/config in a script that is run from cron

pwd returns the caller's directory, not the script's. Cron starts jobs in the user's home, so the path is wrong.

DIR=$(cd "$(dirname "$0")" && pwd -P)/config
docker run -v $(pwd):/app image when the path has spaces

The unquoted substitution splits on spaces, so docker gets broken arguments.

docker run -v "$(pwd)":/app image (or "$PWD":/app)

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create a symlink ~/cfg pointing at ~/app/config, cd through it, then compare pwd, pwd -P and /bin/pwd.

  2. 2

    Print the current and previous directory using only shell variables, no commands.

  3. 3

    Find the working directory of your own shell process through /proc.

Related commands

cdChange the current working directorylnCreate hard links and symbolic linkslsList directory contentsbash: variablesShell variables: assign, quote, default and export
PreviouscdNextcp
Linux / File & Directory Management / pwd

pwd

Print the current working directory

Beginneressentialnavigationscripting

pwd prints the full path of the directory you are in. It sounds trivial, but knowing the difference between the logical path (-L, with symlink names) and the physical one (-P) matters when deploys switch symlinks or a directory is deleted under you.

Syntaxpwd [-L | -P]

Watch it run

You've jumped around between directories over SSH and want to be sure where the next command will run.

devops@devops-lab: /var/log — zsh — 96×28
➜ /var/log
Step 1 of 4
pwd

pwd prints the absolute path from / of the current directory. Run it before anything destructive like rm or chmod -R.

Anatomy of a command

$
builtincd

Change into the directory computed by the substitution that follows.

How it works

There are two pwds: the shell builtin (what you get when you type pwd) and /usr/bin/pwd from coreutils. The bash builtin defaults to -L and prints $PWD, the logical path bash has tracked as you cd, symlink names included. Builtin pwd -P resolves the symlinks in that remembered string. /usr/bin/pwd asks the kernel instead: the getcwd() system call walks from the process's current directory inode up through the .. entries to / and returns the real path. The kernel keeps a reference to your working directory, not a path, so if someone deletes or renames the directory you are in, the builtin still prints the old $PWD while getcwd() fails or returns a different name. For other processes, /proc/PID/cwd (or pwdx PID) gives the same physical answer.

Options you'll actually use

pwdPrint the logical working directory (bash default: $PWD)
-LLogical: keep symlink names as you typed them
-PPhysical: resolve the symlinks in the path (builtin: in $PWD)
/usr/bin/pwdThe coreutils binary: physical by default, asks the kernel
$PWD / $OLDPWDShell variables holding the current and previous directory
pwdx PIDPrint another process's working directory (procps)

Common mistakes

Assuming pwd shows the real directory when you entered through a symlink

The builtin prints the logical path (-L), which keeps symlink names; even builtin -P only resolves the remembered name, which can be stale after the link moves.

/usr/bin/pwd or readlink /proc/$$/cwd for the kernel's answer
DIR=`pwd`/config in a script that is run from cron

pwd returns the caller's directory, not the script's. Cron starts jobs in the user's home, so the path is wrong.

DIR=$(cd "$(dirname "$0")" && pwd -P)/config
docker run -v $(pwd):/app image when the path has spaces

The unquoted substitution splits on spaces, so docker gets broken arguments.

docker run -v "$(pwd)":/app image (or "$PWD":/app)

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create a symlink ~/cfg pointing at ~/app/config, cd through it, then compare pwd, pwd -P and /bin/pwd.

  2. 2

    Print the current and previous directory using only shell variables, no commands.

  3. 3

    Find the working directory of your own shell process through /proc.

Related commands

cdChange the current working directorylnCreate hard links and symbolic linkslsList directory contentsbash: variablesShell variables: assign, quote, default and export
PreviouscdNextcp