The ‘pwd’ command is extremely useful for individuals operating within the Linux command line. It provides information about the current directory you are in, starting from the root (/) directory. This command is particularly valuable for Linux beginners who may find themselves disoriented amidst various directories when using the command line interface. Fortunately, the ‘pwd’ command can help them navigate and regain their bearings.

What is pwd?

The acronym ‘pwd’ stands for ‘Print Working Directory.’ True to its name, the ‘pwd’ command prints out the current working directory, indicating the directory in which the user is currently located. It displays the name of the current directory along with the complete path, starting from the root directory (/). This command is an integral part of the shell and is available in various shells such as bash, Bourne shell, ksh, zsh, and more.

Basic syntax of pwd:
# pwd [OPTION]
Options used with pwd
 Options Description
 -L (logical) Use PWD from environment, even if it contains symbolic links
 -P (physical) Avoid all symbolic links
 –help Display this help and exit
 –version Output version information and exit

If both ‘-L‘ and ‘-P‘ options are used, option ‘L‘ is taken into priority. If no option is specified at the prompt, pwd will avoid all symlinks, i.e., take option ‘-P‘ into account.

Exit status of command pwd:

0Success
Non-zeroFailure

This article aims at providing you a deep insight of Linux command ‘pwd‘ with practical examples.

1. Print your current working directory.

avi@tecmint:~$ /bin/pwd

/home/avi

To create a symbolic link of the folder /var/www/html as htm in your home directory and then navigate to it, you can follow these steps:

Open a terminal or command line interface.

2. Use the ln command with the -s option to create a symbolic link. Run the following command:

   ln -s /var/www/html ~/htm

This command creates a symbolic link named htm in your home directory (~) that points to the /var/www/html folder.

To navigate to the newly created directory, execute the following command:

   cd ~/htm

You are now inside the symbolic link directory (htm) that points to /var/www/html.

To print the working directory with symbolic links, use the pwd command:

   pwd -P

This will display the complete path of the current directory, resolving any symbolic links along the way.

To print the working directory without resolving symbolic links, simply use the pwd command without any options:

   pwd

This will display the symbolic link path itself, without resolving it to the actual target directory.

By following these steps, you can create a symbolic link of the /var/www/html folder as htm in your home directory, navigate to it, and print the working directory with and without symbolic links.

3. Print working directory from environment even if it contains symlinks.

avi@tecmint:~$ /bin/pwd -L

/home/avi/htm

4. Print actual physical current working directory by resolving all symbolic links.

avi@tecmint:~$ /bin/pwd -P

/var/www/html

5. When the pwd command is executed without any options, it does not automatically take into account the -P option. The default behavior of pwd without any options is to print the working directory as it is, without resolving symbolic links.

avi@tecmint:~$ /bin/pwd

/var/www/html

Result: It’s clear from the above output of example 4 and 5 (both result are same) thus, when no options are specified with command “pwd”, it automatically takes option “-P” into account.

6. Print version of your ‘pwd’ command.

avi@tecmint:~$ /bin/pwd --version

pwd (GNU coreutils) 8.23
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Jim Meyering.

You are correct. The ‘pwd’ command is often used without options and is not typically used with arguments.

There is a distinction between running ‘pwd’ and ‘/bin/pwd’. When you execute ‘pwd’ alone, you are invoking the shell’s built-in version of the ‘pwd’ command. The behavior and available options may vary depending on the specific shell you are using. It’s recommended to refer to the manual or documentation for your shell to understand the exact behavior of the built-in ‘pwd’ command in your specific shell.

On the other hand, when you use ‘/bin/pwd’, you are explicitly calling the binary version of the command located at ‘/bin/pwd’. This binary version typically provides additional options and functionalities compared to the shell’s built-in ‘pwd’. You can refer to the manual or documentation for the binary version to explore the available options and their usage.

Both the shell built-in ‘pwd’ and the binary version serve the purpose of printing the current working directory, but the binary version often offers more extensive capabilities.

7. Print all the locations containing executable named pwd.

avi@tecmint:~$ type -a pwd

pwd is a shell builtin
pwd is /bin/pwd

8. To store the output of the ‘pwd’ command in a variable and print its value from the variable, you can follow these steps in a shell script:

#!/bin/bash

a=$(pwd)
echo "Current working directory is: $a"

When you run this shell script, it will assign the output of the ‘pwd’ command to the variable ‘a’ using the syntax a=$(pwd). Then, it will print the value of the ‘a’ variable along with a descriptive message using the ‘echo’ command.

After executing the script, the output will be similar to:

Current working directory is: /home/avi

In shell scripting, storing the output of a command in a variable is useful for capturing the result and using it in subsequent operations or displaying it as needed.

Alternatively, we can use printf, in the above example.

9. Change current working directory to anything (say /home) and display it in command line prompt. Execute a command (say ‘ls‘) to verify is everything is OK.

avi@tecmint:~$ cd /home
avi@tecmint:~$ PS1='$pwd> '		[Notice single quotes in the example]
> ls

10. To set a multi-line command line prompt, you can customize the value of the PS1 environment variable. Here’s an example of how you can achieve this:

avi@tecmint:~$ PS1=$'\n$PWD\n$ 123#Hello#!\n$ '

The above command sets the PS1 variable to a multi-line prompt with the desired format. The $PWD variable represents the current working directory, and the line 123#Hello#! can be customized as per your preference.

After setting the multi-line prompt, you can execute commands as usual, such as ls, to check if everything is working correctly. Here’s an example:

/home
123#Hello#!
file1.txt  file2.txt  directory1

The output of the ls command is displayed after the multi-line prompt, confirming that everything is functioning correctly.

Remember that setting the PS1 variable will only affect the current session. If you want to make the change permanent, you can add the command to your shell’s configuration file (e.g., ~/.bashrc for Bash) or customize your shell startup files accordingly.

11. Check the current working directory and a previous working directory in one GO!

avi@tecmint:~$ echo “$PWD $OLDPWD”

/home /home/avi

12.  What is the absolute path (starting from /) of the pwd binary file.

/bin/pwd 

13. What is the absolute path (starting from /) of the pwd source file.

/usr/include/pwd.h 

14. Print the absolute path (starting from /) of the pwd manual pages file.

/usr/share/man/man1/pwd.1.gz

15. Write a shell script analyses current directory (say tecmint) in your home directory. If you are under directory tecmint it output “Well! You are in tecmint directory” and then print “Good Bye” else create a directory tecmint under your home directory and ask you to cd to it.

Let’s first create a ‘tecmint’ directory, under it create a following shell script file with name ‘pwd.sh’.

avi@tecmint:~$ mkdir tecmint
avi@tecmint:~$ cd tecmint
avi@tecmint:~$ nano pwd.sh

Next, add the following script to the pwd.sh file.

#!/bin/bash

x="$(pwd)"
if [ "$x" == "/home/$USER/tecmint" ]
then
     {
      echo "Well you are in tecmint directory"
      echo "Good Bye"
     }
else
     {
      mkdir /home/$USER/tecmint
      echo "Created Directory tecmint you may now cd to it"
     }
fi

Give execute permission and run it.

avi@tecmint:~$ chmod 755 pwd.sh
avi@tecmint:~$ ./pwd.sh

Well you are in tecmint directory
Good Bye

Conclusion

Absolutely! The ‘pwd’ command is indeed one of the simplest yet essential commands in Linux. It provides valuable information about the current working directory, enabling users to navigate the Linux terminal effectively.

Having a good command over ‘pwd’ is fundamental for using the Linux terminal efficiently. It helps users maintain awareness of their current location within the directory structure, especially when working with complex file systems or executing commands that involve relative paths.

Thank you for sharing this information, and I look forward to assisting you with any other interesting articles or topics in the future. Stay tuned and connected to Serverconfig for more Linux-related knowledge and insights!

Comments to: 15 ‘pwd’ (Print Working Directory) Command Examples in Linux

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