pwd Command - Print Working Directory


Table of contents:

Introduction

  • Introduction
  • Usage
  • Syntax
  • What does pwd command do?
  • Option -L
  • Option -P
  • Where is pwd binary located?
  • Exit codes
  • Conclusion

One of the very first commands that one learns when starting with Linux or Unix Operating Systems is the pwd command.

pwd stands for print working directory.

It is a shell built-in general-purpose command.

The pwd command example GIF

Usage:

To display the current working directory name

Syntax:

pwd [-L | -P]

What does pwd command do?

When you execute the pwd command on any shell prompt, it will print to the standard output (stdout) the name of the current directory that the user is in.

The name is displayed starting from the root directory (i.e. the / (slash) at the start of the name) followed by slashes that denotes separation between directories (nested directories)

The name after the last slash is the name of your current directory you are in.

Example 1:
# pwd
/Users/code2care/Desktop

In the above example Desktop is the current directory located within User -> code2care.

Example 2:
# pwd
/

If you just get / as output then you are in the root directory.


Options:

There are only two options that you can use with the pwd command i.e. -L and -P

-L (--logical)

When -L option is used with pwd command it will display the logical current working directory, which is the value of the PWD environment variable.

Example 3:
# pwd -L
/home/mydirs

-P (--physical)

When -P option is used with the pwd command it will display the symbolic link if it is associated with the current directory, if not, it will simply print out the PWD value as with -L option.

Example 4:
# cd /home/mydirs

# pwd -P
/home/dir1/dir2/dir3

As you can see in the above example, we changed the directory to /home/mydirs which is actually a symbolic link to /home/dir1/dir2/dir3 hence -P options prints out /home/dir1/dir2/dir3 instead of /home/mydirs.

Note: The -L option will not work if the PWD environment variable is exported by the shell.


Where is pwd binary located?

The pwd file/binary is located in /bin/pwd


Error codes associated with PWD command

0 -> success
> 0 -> an error occurs

If you provide an incorrect option/flag, you will get an error,

# pwd -A
pwd: bad option: -A
# pwd -X
bash: pwd: -X: invalid option
pwd: usage: pwd [-LP]
PWD Command Error

Conclusion

To know where are you currently when working on a Terminal Shell, make use of pwd command. Use -P option to know the real physical location of the current directory.

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap