Home > Articles > Operating Systems, Server > Linux/UNIX/Open Source

This chapter is from the book

This chapter is from the book

Display the Path of Your Current Directory

pwd

Of course, while you’re listing the contents of directories hither and yon, you might find yourself confused about just where you are in the file system. How can you tell in which directory you’re currently working? The answer is the pwd command, which stands for print working directory.

The pwd command displays the full, absolute path of the current, or working, directory. It’s not something you’ll use all the time, but it can be incredibly handy when you get a bit discombobulated.

$ pwd
/home/scott/music/new

There is one thing you should be aware of, however, and this can really confuse people. In Chapter 3 you’re going to find out about the ln command (“Create a Link Pointing to Another File or Directory”), so you might want to skip ahead and read that to fully understand what I’m about to show you. Assuming you have, check out the following:

# ls -l
lrwxrwxrwx  scott scott  websites -> /var/www/
$ cd websites
$ pwd
/websites
$ pwd -P
/var/www

So there’s a soft link with a source websites that points at a target /var/www. I cd using the soft link and enter pwd. Notice what comes back: the logical directory of /websites, the source of the soft link, which isn’t the actual working target directory of /var/www. This is the default behavior of pwd, equivalent to entering pwd -L (or --logical).

On the other hand, if you enter pwd -P (or --physical), you instead get back the target of the soft link, which is /var/www.

I don’t know about you, but when I use pwd, I usually want to get back the actual physical location (the target), not the logical location (the source). Since I actually prefer the -P option as the default, I like to use an alias in my .bash_aliases file (discussed in Chapter 12’s “Create a New Permanent Alias” section) that looks like this:

alias pwd="pwd -P"

Now you understand not only how to use pwd, but also the gotchas you might run into when you use it.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.