sandra_henrystocker
Unix Dweeb

Don’t let symbolic links mislead you

How-To
May 21, 20152 mins

Where am I?

Ever notice how, after following a symbolic link to a new directory, the pwd command shows you where you are in the file system according to the symbolic link rather than the actual file system location? This used to bother me until I discovered that there’s an option for the pwd command that will cause the command to display your actual location and it’s -P.

Symbolic links are handy, especially if you’re wending your way into a directory that is deeply buried in the file system. I, for example, often have to work my way to a logs directory located in a place such as /home/oracle/jboss-4.2.2.GA/server/default/deploy/aveksa.ear/aveksa.war/log. That’s a lot to type even with auto-completion. Instead, I’ll make a symlink and see this:

$ cd
$ ln -s /home/oracle/jboss-4.2.2.GA/server/default/deploy/aveksa.ear/aveksa.war/log logdir
$ cd logdir
$ pwd
/home/oracle/logdir

When I add the -P option, I get to see the actual location.

$ pwd -P
/home/oracle/jboss-4.2.2.GA/server/default/deploy/aveksa.ear/aveksa.war/log

Obviously, in this case, I could look into my history and find my ln -s command but, in most cases, I set up the link months or years ago. Besides, I want my system to provide me with the answer I’m looking for and pwd -P does that for me.

By the way, this works for bash and ksh, but not csh.

sandra_henrystocker

Sandra Henry-Stocker was a programmer, Linux systems administrator, security engineer and Linux journalist for most of her 30-year career. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders. Tune into her 2-Minute Linux video tutorials and take command of your command line.

More from this author