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 22.04 LTS.

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

pwd

Here's an example:

himanshu@ansh:~$ 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 built into the shell, 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 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).

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

As you can see, pwd is a fairly easy command line tool to understand and use. Mostly, you'll find yourself using it sans any option, but it's good to know what options it offers. For more info on pwd, head to its man page here.

Share this page:

0 Comment(s)