The pwd command in Linux prints the complete path of the current working directory. PWD is short for Print Working Directory. It comes in handy to check the directory you are currently operating from. The command to change directories is cd.
Basics of the pwd command
Using pwd command is really easy. Type in the command in the terminal and you’ll get the output as the current working directory..
The terminal outputs root as the current working directory. Let’s cd into some other directory and try running pwd again.
$ cd test_directory $ pwd
Now we get the output as /root/test_directory. To come out of any directory use the command:
$ cd ..
Let’s try creating a new directory and cd into it.
$ mkdir test_pwd $ cd test_pwd
Now, let’s use pwd to print the current working directory.
We get the output we expected. The pwd command is a nice way to check the directory you are in before creating new files.
Demonstrating pwd flags
The pwd command gives the option of using two flags. These two flags are P and L.
- pwd -P : (physical) This prints the actual path by resolving the symbolic link to the original path
- pwd -L : (logical) Displays the real path, even if it’s a symbolic link for another location
A symbolic link is a type of file in Linux that points to another file or a folder on your system. It is also known as symlink. These are very similar to shortcuts on Windows. The command to create symlink is ln.
1. Create a symbolic link
To understand the use of these two flags better, let’s create a symlink for a directory.
$ ln -s test_directory symlink
-s here specifies that the link should be soft. The other type of symbolic link is hard.
2. Testing out both the flags
Let’s cd into our newly created symlink.
$ cd symlink
Once we are inside the directory. We can use pwd with -L and -P flags to see the difference.
$ pwd -P $ pwd -L
As we can see that pwd -P resolves the symlink, whereas pwd -L does not.
What does default pwd behave as?
The default behavior of built-in pwd is same as pwd -L. That is to say it does not resolve symlinks.
We can see it in the following example.
That is it for pwd command. It comes in handy to find out the current working directory. To know more about the pwd command you can read its man page.