How to Use pwd Command in Linux: 9 Practical Examples

PWD (Print Working Directory) is a fundamental Linux command to print the current working directory. pwd Command is an essential tool for navigating the file system and obtaining the full path of the working directory and even resolving symbolic links.

In this blog post, we will discuss the pwd command in detail with practical examples to follow in real-life situations.

Understanding the Basics of the pwd Command

Print working directory in shorts pwd command is useful to navigating Linux filesystem. It provides the absolute path of the directory you are currently in. When working with complex file structures, this is a very useful command to know your position in the file hierarchy. It is one of the most useful tools for scripting. With the help of the pwd command, you can define dynamic paths and reference any path in your file system.

Syntax and Usage of the pwd Command

The basic pwd command is straightforward to use. You just need to type pwd in the terminal, and it will print the absolute path of the working directory

$ pwd
/home/ashiqur/Documents

In my case, the pwd command print output of my working directory.

How the pwd Command Relates to the File System

To understand how the pwd command works, you need to know the file system hierarchy in Linux. In Linux, directories are organized in a tree-like structure where “/” is the root directory, and every other folder or directory is a branch root. So when you execute the pwd command in Linux, it shows a tree-like hierarchy starting from “/” Here is a reference hierarchy of Linux file hierarchy.

/
├── home
│   ├── user
│   │   └── documents
│   ├── guest
│   └── admin
├── var
│   ├── log
│   └── www
└── usr
    ├── bin
    ├── lib
    └── include

For my previous example, my working directory is Documents, a branch of the ashiqur directory. So, when I enter the pwd command in the terminal, I get output /home/ashiqur/Documents.

This concept is useful for understanding the pwd command and will help you directly determine the location without executing the pwd command.

pwd Command Options and Flags

Although the basic pwd command is straightforward, the pwd command supports several options and flags that can be used to customize the pwd command output.

OptionDescription
-LThis option will print the logical path of the current working directory even if it contains symbolic links.
-PThis option is opposite to -L and instructs the pwd command to resolve the symbolic links and display the absolute physical path of the working directory.
–helpThis option is for a quick manual page. You will find basic options and flags with a short description.

Practical Examples pwd Command

There are several use cases of the pwd command in real-life scenarios. In this section, we will explore pwd command with practical examples.

Example 1: Checking The Current Working Directory

The most common use case of the pwd command is quickly checking the current working directory. This is useful if you are navigating to the file system.

pwd

The output will display the absolute path of the current working directory.

pwd command example

Example 2: L Flag: Display The Logical Path

The -L option displays the logical path of the current working directory even if you are in the symbolic links.

Symbolic links are special files that act as a pointer to another file. If you navigate to the symbolic folder and want to know the relative logical path of the symbolic link without resolving the actual physical path. Then use the following command

pwd -L

The output will display the logical path of symbolic links instead of the actual physical path.

pwd command example with L option

When working with symbolic links is often necessary to know your actual physical path instead of a relative one. In this case -P flag is used. This flag displays the physical location instead of the logical location of any working directory

pwd -P

The output will display the actual physical path by resolving symbolic links.

pwd command resolving smbolic links

Example 4: List All Files With Absolute Path

Combining pwd with other commands enables you to obtain the full advantage of the pwd command. For example, if you want to list all the files in the current working and its folder and subfolder with their absolute path, then you can combine pwd command with the find command to accomplish this

find $(pwd)

The output will show all the files along with their absolute path

combine pwd with find command

Example 5: Incorporating pwd into Scripts

pwd command is a simple yet powerful tool for scripting. You can unleash the full power of the pwd command in scripting. The main advantage of the pwd command in scripting is the ability to dynamically reference the current working directory without providing the actual directory.

For example, you can use pwd command to create a simple backup script.

#!/bin/bash

# Get the current working directory
current_dir=$(pwd)

# Backup directory
backup_dir="/home/user/to/backup"

# Copy files to the backup directory
cp -r $current_dir $backup_dir

This is a simple script to back up the current working directory to the Backup directory.

Example 6: Get Help About pwd Command

If you quickly need to see the available option and flag that provide by the pwd command, then you can use the – – help flag

pwd --help

The output will show available options to use with the pwd command, along with a short description.

pwd command help

Example 7: Access Manual of pwd Command

If you want to know more about pwd command, you can check the manual of pwd with the following command

man pwd

Example 8: Get The Version of pwd Command

To know the pwd command you are currently using, you can use the following command

pwd --version

Conclusion

pwd command is a fundamental command in Linux. Although using the pwd command is easy, it provides some advanced options. In this blog post, We covered the basics of the pwd command with 9 practical examples.

Share your love

Newsletter Updates

Stay updated with our latest guides and tutorials about Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *