Skip to content

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

  1. Basic Usage: Simply typing pwd will display the current working directory.
    Example: pwd

  2. Using with a script: You can incorporate pwd into shell scripts to log the current directory.
    Example: echo "Current directory: $(pwd)"

  3. Redirecting output: You can redirect the output of pwd to a file for record-keeping.
    Example: pwd > current_directory.txt

  4. Combining with other commands: You can use pwd in combination with other commands to navigate or manage files.
    Example: cd $(pwd)/myfolder

  5. Using in a function: Create a function in your shell that utilizes pwd to display the directory.
    Example: function showdir() { pwd; }

  6. Checking before changing directories: Use pwd to confirm your current location before changing directories.
    Example: pwd; cd myfolder

  7. 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"

  8. Debugging: Use pwd to debug scripts by showing the current path at various points.
    Example: echo "Debug: $(pwd)"

  9. Using with environment variables: Store the output of pwd in an environment variable for later use.
    Example: CURRENT_DIR=$(pwd)

  10. 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