Overview of pwd
`pwd` command tells the user the absolute path of the directory they are in within the current terminal. This is very useful when writing shell scripts to calculate relative paths based on the current location or when you get lost in a complex directory structure.
Main Functions of pwd
`pwd` command is primarily used for the following purposes:
Key Use Cases
- Check Current Location: Quickly check where the current directory is while navigating the file system.
- Shell Scripts: Dynamically construct file paths by referencing the current execution location within scripts.
- Prevent Errors: Avoid mistakes of executing files or commands from the wrong location.
Logical Path vs Physical Path
`pwd` command essentially displays the 'logical' path. This means it does not follow symbolic links and shows the path as it was traversed using the `cd` command. It can show references to special directories like `.` or `..` without interpreting them.
Key pwd Command Options
`pwd` command is used without options by default, but it provides two main options regarding how to handle symbolic links.
1. Basic Usage and Handling of Symbolic Links
Generated command:
Try combining the commands.
Description:
`pwd` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Learn how to check your current location and understand the differences in paths in symbolic link situations through various usage examples of the `pwd` command.
Check Current Working Directory
pwd
The most basic usage of the `pwd` command, outputs the full path of the directory where the current terminal is located.
Check Path After Moving Through Symbolic Link (Logical Path)
mkdir real_dir
ln -s real_dir link_to_dir
cd link_to_dir
pwd
Create a directory named `real_dir`, generate a symbolic link `link_to_dir` to it, then move to `link_to_dir` and execute the `pwd` command. By default, the path of `link_to_dir` will be displayed.
Check Path After Moving Through Symbolic Link (Physical Path)
mkdir real_dir
ln -s real_dir link_to_dir
cd link_to_dir
pwd -P
In the same situation as above, use the `-P` option to output the actual physical path (`real_dir`) on the disk.
Store Current Path in a Variable in a Script
current_path=$(pwd)
echo "Current working path: $current_path"
mkdir "$current_path/new_sub_dir"
An example of storing the path of the current working directory in a variable within a shell script for use in other commands.
Check Path While Moving Between Directories
pwd
cd ..
pwd
cd -
Use `pwd` to see how the path changes while moving up to the parent directory.