Pwd command linux usage examples
Command definition
The pwd command stands for "print working directory" and is used in Linux and Unix-like operating systems to display the current directory in which the user is working. When executed, it outputs the full path to the current directory, allowing users to confirm their location within the filesystem hierarchy.
Usage examples
-
Basic Usage: Simply typing
pwdwill display the current working directory.
Example:pwd -
Using with a script: You can incorporate
pwdinto shell scripts to log the current directory.
Example:echo "Current directory: $(pwd)" -
Redirecting output: You can redirect the output of
pwdto a file for record-keeping.
Example:pwd > current_directory.txt -
Combining with other commands: You can use
pwdin combination with other commands to navigate or manage files.
Example:cd $(pwd)/myfolder -
Using in a function: Create a function in your shell that utilizes
pwdto display the directory.
Example:function showdir() { pwd; } -
Checking before changing directories: Use
pwdto confirm your current location before changing directories.
Example:pwd; cd myfolder -
In a script for automation: Use
pwdin scripts to automate tasks that depend on the current directory.
Example:script_dir=$(pwd); echo "Running script in $script_dir" -
Debugging: Use
pwdto debug scripts by showing the current path at various points.
Example:echo "Debug: $(pwd)" -
Using with environment variables: Store the output of
pwdin an environment variable for later use.
Example:CURRENT_DIR=$(pwd) -
Verifying directory before operations: Use
pwdto ensure you are in the correct directory before performing file operations.
Example:if [ "$(pwd)" == "/home/user/myfolder" ]; then echo "In correct folder"; fi