pwdPrint the current working directory
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.
pwd [-L | -P]You've jumped around between directories over SSH and want to be sure where the next command will run.
cdChange into the directory computed by the substitution that follows.
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.
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 directorypwdx PIDPrint another process's working directory (procps)Assuming pwd shows the real directory when you entered through a symlinkThe 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 answerDIR=`pwd`/config in a script that is run from cronpwd 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)/configdocker run -v $(pwd):/app image when the path has spacesThe unquoted substitution splits on spaces, so docker gets broken arguments.
docker run -v "$(pwd)":/app image (or "$PWD":/app)Try these on a Linux machine of your own. Each task has a hint and a solution.
Create a symlink ~/cfg pointing at ~/app/config, cd through it, then compare pwd, pwd -P and /bin/pwd.
Print the current and previous directory using only shell variables, no commands.
Find the working directory of your own shell process through /proc.
pwdPrint the current working directory
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.
pwd [-L | -P]You've jumped around between directories over SSH and want to be sure where the next command will run.
cdChange into the directory computed by the substitution that follows.
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.
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 directorypwdx PIDPrint another process's working directory (procps)Assuming pwd shows the real directory when you entered through a symlinkThe 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 answerDIR=`pwd`/config in a script that is run from cronpwd 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)/configdocker run -v $(pwd):/app image when the path has spacesThe unquoted substitution splits on spaces, so docker gets broken arguments.
docker run -v "$(pwd)":/app image (or "$PWD":/app)Try these on a Linux machine of your own. Each task has a hint and a solution.
Create a symlink ~/cfg pointing at ~/app/config, cd through it, then compare pwd, pwd -P and /bin/pwd.
Print the current and previous directory using only shell variables, no commands.
Find the working directory of your own shell process through /proc.