Which Linux command can be used to display the name of the current working directory chmod Sudo PS pwd?

Introduction

The pwd Linux command prints the current working directory path, starting from the root (/). Use the pwd command to find your way in the Linux file system structure maze or to pass the working directory in a Bash script.

In this tutorial, you will learn to use the pwd command.

Which Linux command can be used to display the name of the current working directory chmod Sudo PS pwd?

Prerequisites

  • A system running Linux
  • Access to the terminal window (Ctrl+Alt+T)
  • A text editor of your choice

pwd Syntax

The pwd command takes the following syntax:

pwd [-options]

The available options are discussed in the following section.

Exit Status

The pwd command has two exit statuses:

  • 0. Success.
  • Non-zero. Failure.

pwd Options

The options provide additional functionality to the command. The pwd command accepts the following options:

OptionDescription
-L, --logical Instructs pwd to output the $PWD environment variable contents, including symbolic links. If no option is specified, pwd assumes -L.
-P, --physical Prints the path to the current directory. All the components are directory names, and symbolic links are resolved.
--version Outputs the program version.
--help Displays the help message.

There are several ways to use the pwd command. The examples below explain common use cases for the pwd command.

Example 1: Get Working Directory Path

Running the pwd command without any options outputs the full path to the current working directory.

For example:

Which Linux command can be used to display the name of the current working directory chmod Sudo PS pwd?

The command outputs the current working directory absolute path. In this case, the home directory path.

Note: An absolute pathname starts with the / character, which marks the root of the file system tree.

However, if you followed a symlink to the current directory, running pwd prints the symlink location and not its destination.

In the following example, we created a symbolic link to the current directory. Following the link and running the pwd command displays the symbolic link in the path:

Which Linux command can be used to display the name of the current working directory chmod Sudo PS pwd?

Example 2: Using the -P Option

The -P option instructs pwd to print the physical working directory and avoids listing symbolic links. Therefore, the command prints the absolute path to the current working directory.

For example:

Which Linux command can be used to display the name of the current working directory chmod Sudo PS pwd?

Even though a symlink was used to change the directory, the pwd command only prints the actual name of the working directory.

Example 3: Using the -L Option

The -L option instructs pwd to print the working directory path, including any symlinks. In the following example, we used a symlink to navigate to a directory and ran pwd -L:

Which Linux command can be used to display the name of the current working directory chmod Sudo PS pwd?

The output lists the path with the symbolic link.

Example 4: Print $PWD Variable Contents

The $PWD environment variable stores the current directory path. Print the variable contents by running:

echo $PWD

Which Linux command can be used to display the name of the current working directory chmod Sudo PS pwd?

The command outputs the current working directory, including any symlinks, stored in the $PWD variable.

Example 5: Personalize pwd Output

Personalize the pwd command output by storing its value in a variable and adding a message using the echo command.

The following example is a script where pwd's value is stored in the $p variable and includes an output message.

Follow the instructions below:

1. Open the terminal and create the script:

vi directory.sh

2. Add the following lines to the script:

#!/bin/bash
p=$(pwd)
echo "You are currently in: $p"

3. Save the script and exit vi with the following command:

:wq

4. Change file permissions to make the script executable:

chmod +x directory.sh

5. Run the script:

./directory.sh

Which Linux command can be used to display the name of the current working directory chmod Sudo PS pwd?

The script outputs the current working directory with a personalized message.

Example 6: Check pwd Version

The pwd command is a built-in shell command (pwd) and an actual binary (/bin/pwd). The shell version may differ from the binary version.

Check which pwd version is installed by running:

/bin/pwd --version

Which Linux command can be used to display the name of the current working directory chmod Sudo PS pwd?

The command outputs the binary pwd version on your system.

Note: Use the type command to get more information about the pwd command. Run type -a pwd to get the locations of all executables containing the pwd keyword - both the shell and binary versions.

Example 7: See Previous Working Directory

The $OLDPWD variable stores the previous working directory path. Check your previous location in the filesystem by running:

echo "$OLDPWD"

Which Linux command can be used to display the name of the current working directory chmod Sudo PS pwd?

The command outputs the previous working directory.

Example 8: See Help File

Use the --help option to see the pwd command help file and the available options:

pwd --help

Which Linux command can be used to display the name of the current working directory chmod Sudo PS pwd?

The output is a brief help file with a list of available options.

Example 9: Use pwd in Scripting

In this example, we created a bash case statement that uses pwd to output the current or previous working directory, depending on the option selected. Follow the steps below:

1. Create a script using a text editor such as vi/vim:

vi directory.sh

2. Enter the lines below:

#!/bin/bash
echo "I need to see:"
echo "1 - My current physical directory path."
echo "2 - My current directory path, including symlinks."
echo "3 - My previous directory"
read directory
case $directory in
1) echo "Your current physical location is: $(pwd -P)";;
2) echo "Your current directory, including symlinks, is: $(pwd -L)";;
3) echo "You were previously in: $OLDPWD";;
esac

3. Save and quit vi by running:

:wq

4. Make the script executable with chmod:

chmod +x directory.sh

5. Execute the script and choose an option:

./directory.sh

For example:

Which Linux command can be used to display the name of the current working directory chmod Sudo PS pwd?

The script utilizes a case statement to offer three options and prints a corresponding output based on the answer.

6. Change the directory using a symlink, execute the script again and compare the outputs:

Which Linux command can be used to display the name of the current working directory chmod Sudo PS pwd?

The output shows the physical location, even though we used a symlink to change the directory.

7. Test the remaining options as well to see the result:

Which Linux command can be used to display the name of the current working directory chmod Sudo PS pwd?

Example 10: Set up an Alias for pwd

Create an alias that includes the -P option to prevent getting symlinks when running pwd. That way, pwd always shows the path to the directory you're in, regardless of how you got there.

In the following example, the alias includes the -P option and we have added it to the .bashrc file to make the system remember the alias after a system reboot.

alias pwd='pwd -P'; echo "alias pwd='pwd -P'" >> ~/.bashrc

The output displays the physical location, although we followed a symlink to the current path.

Conclusion

This tutorial showed how to use the pwd command in Linux to check your location quickly.

If you are interested in bash, see our tutorial for bash functions and how to use them. Alternatively, we recommend delving further into Linux commands in our list of all important Linux commands in one place.

Which Linux command can be used to display the name of the current working directory pwd sudo chmod PS?

pwd (print working directory) – The pwd command is used to display the name of the current working directory in the Linux system using the terminal. This is a shell building command that is available in most Unix shells such as Bourne shell, ash, bash, kash, and zsh.

Which Linux command can be used to display he name of the current working directory?

The pwd command can be used to determine the present working directory. and the cd command can be used to change the current working directory.

What is pwd in Linux command?

The pwd command writes to standard output the full path name of your current directory (from the root directory). All directories are separated by a / (slash). The root directory is represented by the first /, and the last directory named is your current directory.

How do I display the current directory in Linux?

To determine the exact location of your current directory within the file system, go to a shell prompt and type the command pwd. This tells you that you are in the user sam's directory, which is in the /home directory. The command pwd stands for print working directory.