Linux pwd Command Tutorial for Beginners (with Examples)
The pwd command, like ls and cd, is one of the most frequently used Linux utilities. Regardless of the kind of user you are (newbie or pro), you’ll find yourself using this command line tool a lot. So in this tutorial, we will quickly discuss the basics of pwd through some easy to understand examples.
But before we start doing that, it’s worth mentioning that all examples presented in this article have been tested on Ubuntu 18.04 LTS.eval(ez_write_tag([[580,400],’howtoforge_com-medrectangle-3′,’ezslot_2′,121,’0′,’0′]));
Linux pwd command
The pwd tool prints the name of the present/current working directory (PWD – Present Working Directory, got it?). Following is its syntax:
And here’s how the utility’s man page describes it:
Print the full filename of the current working directory.
Following are some Q&A-styled examples that should give you a good idea of how the pwd command works.
Q1. How does pwd command work?
The basic usage, as is usually the case, is very easy. All you have to do is to run the ‘pwd’ command without any options, and you’ll get the full path to the current working directory in output.
Here’s an example:
[email protected]:~$ pwd
It’s worth mentioning that most shells, including bash, have pwd built-in. Just execute the following command:
type -a pwd
And you should get the following output.
pwd is a shell builtin
pwd is /bin/pwd
Q2. What is the difference between pwd and /bin/pwd?
Well, pwd is a built into the shell, while /bin/pwd is the tool that comes with your Linux distribution. PWD’s man page explains it clearly:eval(ez_write_tag([[580,400],’howtoforge_com-medrectangle-4′,’ezslot_1′,108,’0′,’0′]));
NOTE: Your shell may have its own version of pwd, which usually supersedes the version described
here. Please refer to your shell's documentation for details about the options it supports.
Q3. How to make pwd use PWD from the environment?
This can be done using the -L command line option. To test this, create a symlink to the current directory (/home/himanshu in my case).
ln -s . dir_symlink
and then enter it:
Now, run the following command:
In my case, the got the following output:
Next, execute this command
I got the following output:
Q4. How to make pwd avoid all symbolic links?
This can be done using the -P command line option. For example:
produces the following output:
But with -P
/bin/pwd -L -P
the output is:
It’s worth mentioning that If no option is specified, -P is assumed by default.
So you can see, pwd is a fairly easy command line tool to understand and use. Mostly, you’ll find yourself using sans any option, but it’s good to know what all options it offers. For more info on pwd, head to its man page here.