Skip to content

pwd Linux command

The pwd command in Linux stands for “print working directory”. It is used to display the full pathname of the current working directory. This command is helpful for users who need to know their exact location within the file system. By running pwd, you can easily identify where you are in the directory structure. This can be especially useful when navigating through different directories or when writing scripts that require specific file paths. The output of pwd will show the complete path starting from the root directory (”/”) to the current working directory. This command is simple to use and provides a quick way to orient yourself within the file system.

pwd Syntax:

Terminal window
pwd [option]

Options:

OptionDescription
-LDisplay logical path
-PDisplay physical path
—helpDisplay help message
—versionDisplay version information

Parameters:

ParameterDescription
NoneThere are no parameters

pwd bash Examples:

Terminal window
pwd

This command will print the full path of the current working directory.

Store the Current Working Directory in a Variable

Terminal window
current_dir=$(pwd)
echo "The current working directory is: $current_dir"

In this example, the pwd command is used to store the current working directory in a variable and then display it.

Change Directory and Display the New Working Directory

Terminal window
cd /usr/share
pwd

By using pwd after changing the directory, you can display the new working directory.

Pipe Output of pwd to a File

Terminal window
pwd > current_directory.txt

This command redirects the output of pwd to a file named “current_directory.txt”.

List Files in the Current Working Directory Using pwd

Terminal window
ls $(pwd)

Using pwd with ls to list the files in the current working directory.

Check Existence of a Specific Directory

Terminal window
if [ $(pwd) = "/home/user" ]; then
echo "You are in the correct directory."
else
echo "Wrong directory."
fi

This example checks if the current working directory is “/home/user” using the pwd command in a conditional statement.

pwd Command Help Center:

How do I use pwd in Linux?

To use the pwd command in Linux, execute the following command:

Terminal window
pwd

To display the physical path with symbolic links resolved by the pwd command, use the -P option:

Terminal window
pwd -P

How do I get the logical path in pwd?

To get the logical path with symbolic links intact, you can use the -L option with the pwd command:

Terminal window
pwd -L

How can I get the path in pwd as a shell script?

You can assign the output of the pwd command to a variable in a shell script as shown below:

Terminal window
current_directory=$(pwd)
echo "Current directory: $current_directory"

How do I print the path with Bash prompt in pwd?

To print the path along with the Bash prompt using the pwd command, you can use the following Bash prompt customization:

Terminal window
PS1='\w\$ '

How do I save the path into a file using pwd?

To save the output of the pwd command to a file, you can redirect the output using the following command:

Terminal window
pwd > path.txt

How do I only display the directory name using pwd?

To only display the directory name without the full path, you can use the basename command in conjunction with pwd as follows:

Terminal window
basename $(pwd)

If you want to ignore symbolic links in the pwd output and only show the physical directory, you can use the -P option:

Terminal window
pwd -P

Applications of the pwd command

  1. Display the current working directory
  2. Provide the full path of the current working directory
  3. Referencing the current directory in shell scripts
  4. Verifying the location before executing commands