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
pwd
will display the current working directory.
Example:pwd
-
Using with a script: You can incorporate
pwd
into shell scripts to log the current directory.
Example:echo "Current directory: $(pwd)"
-
Redirecting output: You can redirect the output of
pwd
to a file for record-keeping.
Example:pwd > current_directory.txt
-
Combining with other commands: You can use
pwd
in 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
pwd
to display the directory.
Example:function showdir() { pwd; }
-
Checking before changing directories: Use
pwd
to confirm your current location before changing directories.
Example:pwd; cd myfolder
-
In a script for automation: Use
pwd
in scripts to automate tasks that depend on the current directory.
Example:script_dir=$(pwd); echo "Running script in $script_dir"
-
Debugging: Use
pwd
to debug scripts by showing the current path at various points.
Example:echo "Debug: $(pwd)"
-
Using with environment variables: Store the output of
pwd
in an environment variable for later use.
Example:CURRENT_DIR=$(pwd)
-
Verifying directory before operations: Use
pwd
to ensure you are in the correct directory before performing file operations.
Example:if [ "$(pwd)" == "/home/user/myfolder" ]; then echo "In correct folder"; fi