Linux pwd Command Tutorial for Beginners (with Examples)

The pwd command, like ls and cd, is one of 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 16.04LTS.

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:

pwd [OPTION]...

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 pwd command works?

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.

pwd

Here's an example:

[email protected]:~$ pwd
/home/himanshu

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 shell built-in, while /bin/pwd is the tool that comes with your Linux distribution. PWD's man page explains it clearly:

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 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).

For example:

ln -s . dir_symlink

and then enter it:

cd dir_symlink

Now, run the following command:

/bin/pwd

In my case, the got the following output:

/home/himanshu

Next, execute this command

/bin/pwd -L

I got the following output:

/home/himanshu/dir_symlink

This can be done using the -P command line option. For example:

/bin/pwd -L

produces the following output:

/home/himanshu/dir_symlink

But with -P

/bin/pwd -L -P

the output is:

/home/himanshu

It's worth mentioning that If no option is specified, -P is assumed by default.

Conclusion

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.

Himanshu Arora

About Himanshu Arora

Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu's work has also been featured in some of world's other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.

Share this page:

Suggested articles

0 Comment(s)

Add comment