Open In App
Related Articles

How to Display Current Working Directory in Linux | pwd Command

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The ‘pwd,’ which stands for “print working directory.” In this article, we will delve into the ‘pwd’ command, exploring its functionality, usage, and various examples. It prints the path of the working directory, starting from the root. pwd is shell built-in command(pwd) or an actual binary(/bin/pwd). $PWD is an environment variable that stores the path of the current directory. This command has two flags.

Syntax of `pwd` command in Linux

The basic syntax of the ‘pwd’ command is

pwd [OPTIONS]

This command doesn’t have any arguments or options, but it can accept flags for specific behavior. 

Flags For Specific behavior in `pwd` command in Linux.

  • The “-L” flag resolves symbolic links and prints the path of the target directory.
  • The default behavior of the shell built-in “pwd” is equivalent to using “pwd -L”.
  • Mention the “-P” flag, which displays the actual path without resolving symbolic links.
  • The default behavior of the binary “/bin/pwd” is the same as using “pwd -P”

pwd -L: Prints the symbolic path. 

pwd -P: Prints the actual path.

How to Display the Current Working Directory in Linux

1. Displaying the Current Working Directory Using Built-in pwd (pwd):

To print the current working directory, simply enter:

Display the Current Working Directory

Display the Current Working Directory

The output will be the absolute path of your current location in the file system.

In the given example the directory /home/shital/logs/ is a symbolic link for a target directory /var/logs/ 

2. Displaying the Current Working Directory Using Binary pwd (/bin/pwd):

BinaDisplay the Current Working Directory ry pwd (/bin/pwd):

Display the Current Working Directory

The default behavior of Built-in pwd is the same as pwd -L. Using “pwd -L” to obtain the symbolic path of a directory containing a symbolic link. 

 The default behavior of /bin/pwd is the same as pwd -P. Utilizing “pwd -P” to display the actual path, ignoring symbolic links.

3. The $PWD Environment variable. 

The $PWD environment variable is a dynamic variable that stores the path of the current working directory. It holds the same value as ‘pwd -L’ – representing the symbolic path.

$PWD

$PWD

Executing this command prints the symbolic path stored in the $PWD environment variable

Frequently Asked Questions

1) How do I print the current working directory in Linux using the ‘pwd’ command?

You can print the current working directory in Linux by simply entering the ‘pwd’ command in the terminal and pressing Enter. This will display the absolute path of your current location in the file system.

2) What is the difference between ‘pwd’ and ‘/bin/pwd’ in Linux?

The ‘pwd’ command and ‘/bin/pwd’ binary both serve the purpose of printing the current working directory. However, the default behavior differs: ‘pwd’ behaves as if the ‘-L’ option is used, while ‘/bin/pwd’ behaves like ‘pwd -P’, displaying the actual path and ignoring symbolic links.

3) Can I redirect the output of ‘pwd’ to a file in Linux?

Yes, you can redirect the output of the ‘pwd’ command to a file by using the following command:

pwd > filename.txt

This will write the absolute path of the current working directory to the specified file

4) How can I store the current working directory in a variable for use in a Linux script?

You can store the current working directory in a variable in a Linux script by using the following syntax:

current_directory=$(pwd)
echo "The current working directory is: $current_directory"

This captures the output of ‘pwd’ in the variable ‘current_directory’ for later use in your script.

5) What is the significance of the $PWD environment variable in Linux?

The $PWD environment variable in Linux holds the symbolic path of the current working directory. It provides a dynamic way to access and utilize the current directory path in scripts or commands. The value of $PWD is equivalent to the output of ‘pwd -L’.

Conclusion

In this article we discussed the ‘pwd’ command in Linux, which helps you find where you are in your computer’s folders or we can say “how to print the current working directory “. It can show you the real folder path (‘pwd -P’) or the symbolic one (‘pwd -L’). The $PWD thing does the same as ‘pwd -L’ and is handy for scripts. Remember, ‘/bin/pwd’ shows the actual path. The FAQs answered common questions, like how to use ‘pwd’ or save a folder path in a script.


Last Updated : 18 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads