pwd Linux command
The pwd command in Linux stands for “print working directory”. It is used to display the full pathname of the current working directory. This command is helpful for users who need to know their exact location within the file system. By running pwd, you can easily identify where you are in the directory structure. This can be especially useful when navigating through different directories or when writing scripts that require specific file paths. The output of pwd will show the complete path starting from the root directory (”/”) to the current working directory. This command is simple to use and provides a quick way to orient yourself within the file system.
pwd Syntax:
pwd [option]Options:
| Option | Description |
|---|---|
| -L | Display logical path |
| -P | Display physical path |
| —help | Display help message |
| —version | Display version information |
Parameters:
| Parameter | Description |
|---|---|
| None | There are no parameters |
pwd bash Examples:
Print the Current Working Directory
pwdThis command will print the full path of the current working directory.
Store the Current Working Directory in a Variable
current_dir=$(pwd)echo "The current working directory is: $current_dir"In this example, the pwd command is used to store the current working directory in a variable and then display it.
Change Directory and Display the New Working Directory
cd /usr/sharepwdBy using pwd after changing the directory, you can display the new working directory.
Pipe Output of pwd to a File
pwd > current_directory.txtThis command redirects the output of pwd to a file named “current_directory.txt”.
List Files in the Current Working Directory Using pwd
ls $(pwd)Using pwd with ls to list the files in the current working directory.
Check Existence of a Specific Directory
if [ $(pwd) = "/home/user" ]; then echo "You are in the correct directory."else echo "Wrong directory."fiThis example checks if the current working directory is “/home/user” using the pwd command in a conditional statement.
pwd Command Help Center:
How do I use pwd in Linux?
To use the pwd command in Linux, execute the following command:
pwdHow can I display the physical path with symbolic links in pwd?
To display the physical path with symbolic links resolved by the pwd command, use the -P option:
pwd -PHow do I get the logical path in pwd?
To get the logical path with symbolic links intact, you can use the -L option with the pwd command:
pwd -LHow can I get the path in pwd as a shell script?
You can assign the output of the pwd command to a variable in a shell script as shown below:
current_directory=$(pwd)echo "Current directory: $current_directory"How do I print the path with Bash prompt in pwd?
To print the path along with the Bash prompt using the pwd command, you can use the following Bash prompt customization:
PS1='\w\$ 'How do I save the path into a file using pwd?
To save the output of the pwd command to a file, you can redirect the output using the following command:
pwd > path.txtHow do I only display the directory name using pwd?
To only display the directory name without the full path, you can use the basename command in conjunction with pwd as follows:
basename $(pwd)How to ignore the symbolic links in pwd output?
If you want to ignore symbolic links in the pwd output and only show the physical directory, you can use the -P option:
pwd -PApplications of the pwd command
- Display the current working directory
- Provide the full path of the current working directory
- Referencing the current directory in shell scripts
- Verifying the location before executing commands