pwd Command in Linux
Overview
The PWD command in Linux stands for 'print working directory.' It is used to display the current working directory on the screen. In this article, we will go through the syntax, options, example usages, tips, and advanced usages of the PWD command in Linux.
Linux pwd Command Syntax
The syntax for the pwd command is as follows:
Where:
- [OPTION]: This is an optional parameter that determines the behavior of the command. If not specified, it displays the physical current working directory.
- -L: This option displays the logical current working directory.
- -P: This option displays the physical current working directory, which is the default behavior of the command.
pwd Command Options:
- -L: Display the logical current working directory
- -P: Display the physical current working directory (default behavior)
Example Usages
-
Display the physical current working directory:
Output:
Explanation: In this example, the PWD command is used without any options. It displays the physical current working directory, which is '/home/user/Documents'.
-
Display the logical current working directory:
Output:
Explanation: In this example, the PWD command is used with the '-L' option. It displays the logical current working directory, which is also '/home/user/Documents' in this case.
Tips
-
The PWD command is a basic command and does not require any special permissions to be executed.
-
The output of the PWD command is the current working directory, which is useful when navigating the file system.
Advanced Use Cases of pwd Command in Linux
-
Use the PWD command in a script to obtain the current working directory:
Explanation: This command is used to assign the current working directory to a variable 'current_dir' which can be used later in a script. The output of the PWD command is stored in the variable without being displayed on the screen.
-
Redirect the output of the PWD command to a file:
Explanation: This command is used to redirect the output of the PWD command to a file called 'current_directory.txt'. The file will contain the current working directory.
-
Use the PWD command in a pipeline:
Explanation: This command is used to list the files in the current working directory using the 'ls' command. The output of the PWD command is used as an argument to the 'ls' command through the use of '$(pwd)'
Conclusion
-
The PWD command is a useful tool for displaying the current working directory in Linux.
-
The command has two options: '-L' to display the logical current working directory and '-P' to display the physical current working directory.
-
The output of the command can be used in scripts and pipelines to obtain and use the current working directory.
-
Remember that the PWD command is a basic command that does not require any special permissions to execute.