One of the very first commands that one learns when starting with Linux or Unix Operating Systems is the pwd command.
pwd stands for print working directory.
It is a shell built-in general-purpose command.
To display the current working directory name
pwd [-L | -P]
What does pwd command do?
When you execute the pwd command on any shell prompt, it will print to the standard output (stdout) the name of the current directory that the user is in.
The name is displayed starting from the root directory (i.e. the / (slash) at the start of the name) followed by slashes that denotes separation between directories (nested directories)
The name after the last slash is the name of your current directory you are in.Example 1:
# pwd /Users/code2care/Desktop
In the above example Desktop is the current directory located within User -> code2care.Example 2:
# pwd /
If you just get / as output then you are in the root directory.
There are only two options that you can use with the pwd command i.e. -L and -P-L (--logical)
When -L option is used with pwd command it will display the logical current working directory, which is the value of the PWD environment variable.Example 3:
# pwd -L /home/mydirs
When -P option is used with the pwd command it will display the symbolic link if it is associated with the current directory, if not, it will simply print out the PWD value as with -L option.Example 4:
# cd /home/mydirs # pwd -P /home/dir1/dir2/dir3
As you can see in the above example, we changed the directory to /home/mydirs which is actually a symbolic link to /home/dir1/dir2/dir3 hence -P options prints out /home/dir1/dir2/dir3 instead of /home/mydirs.
Note: The -L option will not work if the PWD environment variable is exported by the shell.
Where is pwd binary located?
The pwd file/binary is located in /bin/pwd
Error codes associated with PWD command
0 -> success > 0 -> an error occurs
If you provide an incorrect option/flag, you will get an error,
# pwd -A pwd: bad option: -A
# pwd -X bash: pwd: -X: invalid option pwd: usage: pwd [-LP]
To know where are you currently when working on a Terminal Shell, make use of pwd command. Use -P option to know the real physical location of the current directory.
Have Questions? Post them here!
- ls command: sort files by name alphabetically A-Z or Z-A [Linux/Unix/macOS/Bash]
- How to check uptime of Linux/Unix/macOS system/server using console command?
- How to create tar.gz file using Terminal Command
- How to List All Users in Linux
- How to check file permissions for your file using Linux/Unix/macOS Terminal Command
- How to exit from nano command
- How to go to the End of File in Nano Editor
- ls command sort by file size [Linix/UNIX/macOS/bash]
- scp: ssh: connect to host xxxx port 22: Connection refused Error
- How to tar.gz a directory or folder Command
- Linux: Create a New User and Password and Login Example
- How to install npm on Ubuntu
- Create and write file in single Linux/macOS command
- 3 ways to clear screen on Linux Terminal
- How to ls command to output one entry per line [macOS/Linux/Bash]
- Execute .bin and .run file Ubuntu Linux
- How to install and Configure sar sysstat tools in Ubuntu Linux
- Fix - ssh-copy-id no identities found error
- Terminal display next month Calendar
- Install Java Runtime Environment (Oracle or open JRE) on Ubuntu
- Fix - bash: man: command not found
- How to install curl on Alpine Linux
- Command to unzip or extract tar.gz file on Linux or macOS?
- How to Display content of a file in Terminal Screen?
- Copy entire directory using Terminal Command [Linux, Mac, Bash]
- All directional arrows codes for HTML - Html
- Android Wifi WPA2/WPA Connects and Disconnects issue - Android
- SharePoint Server 2016 setup error - A system restart from a previous installation or update is pending. Restart your computer and run setup to continue. - SharePoint
- [Solution] Notepad++ Compare option unavailable - NotepadPlusPlus
- How to Insert an Inverted Question Mark on Mac ¿ - MacOS
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error - Android
- The package must have at least one . separator [Android Studio] - Android-Studio
- AADSTS90033: A transient error has occurred. Please try again. [Microsoft 365] - Microsoft