Linux Commands: pwd

Linux Commands: pwd

pwd is one of the most helpful commands for linux newbie. Whenever you are lost just type it in your terminal and you will find where are you exactly. Let me show you how you can do it.

Screenshot 2020-10-08 at 11.39.27 PM.png

You can see it tells me my current working directory. Usually, there are two ways you can invoke this command. One way you already saw in the above example. The other way is through /bin/pwd they are almost the same. But pwd is your shell in-built command while /bin/pwd is calling the binary version of that. You can always verify which pwd is getting invoked by using which or type command shown below.

Screenshot 2020-10-08 at 11.47.27 PM.png

pwd has two flags one is -L and -P.

  • -L: Display the logical current working directory.
  • -P: Display the physical current working directory (all symbolic links resolved).

Let's see them in action and understand what exactly one does.

  • We will create a symbolic link of folder /var/log as h in your home directory and move to it.
    $ ln -s /var/log h
    $ cd h
    
  • We will print the working directory using the logical flag pwd -L. It will print the exact working directory from the environment even if it contains symlinks.
    $ pwd -L
    /Users/umesh/h
    
  • After that we will print the working directory using physical flag pwd -P. It will print actual physical current working directory by resolving all symbolic links.
    $ pwd -P
    /private/var/log
    
  • Lastly we will check what happens when we run just pwd. From the output, it's pretty much clear that pwd takes -L as the default flag if no options are provided.
    $ pwd
    /Users/umesh/h
    

Screenshot 2020-10-08 at 11.56.48 PM.png

Conclusion

I will be writing more on these simple and yet useful commands in my coming blog. Linux commands look simple and probably you remember it but if you know a bit in-depth about them they can come in handy.

If you liked this blog please feel free to share with others, if you are sharing it on twitter please tag @imumesh18 . If you are interested in more of these please subscribe to my newsletter.

Did you find this article valuable?

Support Umesh's Blog by becoming a sponsor. Any amount is appreciated!