Learn about pwd command in Linux

Learn about pwd command in Linux

The pwd command in Linux stands for “print working directory.” It is used to display the current directory you are in within the file system hierarchy. This command can be particularly helpful when navigating the command line interface and working with different directories.

Usage:

pwd

Example:

Certainly, here are more examples of using the pwd command in different scenarios:

  1. Basic Usage:
    If you’re in the /home/user directory:
   pwd

Output:

   /home/user
  1. Navigating to a Subdirectory:
    Suppose you’re in the /home/user directory and you navigate to a subdirectory named “projects”:
   cd projects
   pwd

Output:

   /home/user/projects
  1. Using Full Path:
    You can use the pwd command along with other commands that require full paths. For example, creating a file in the current directory:
   touch $(pwd)/newfile.txt

This will create a file named “newfile.txt” in the current directory.

  1. Symbolic Links:
    If you’re in a symbolic link directory that points to another location:
   pwd

Output might show the path of the symbolic link, not the linked location.

  1. Subshell:
    If you use the pwd command within a subshell (enclosed in parentheses), it will give you the path of the current directory without affecting your current location in the main shell:
   (cd /tmp; pwd)

Output:

   /tmp
  1. Using with Variables:
    You can assign the result of pwd to a variable in a shell script:
   current_dir=$(pwd)
   echo "Current directory: $current_dir"

Output:

   Current directory: /home/user
  1. Using Tilde (~) Shortcut:
    When using pwd with the tilde symbol (~), it represents the home directory:
   cd ~
   pwd

Output (assuming your home directory is /home/user):

   /home/user

The pwd command is a simple yet essential tool that helps you keep track of your location within the file system, enabling efficient navigation and manipulation of directories and files.

Leave a Comment

Scroll to Top