pwd Command in Linux with Examples

This tutorial explains Linux “pwd” command, options and its usage with examples.

What is pwd Command?

In Unix-like and some other operating systems, the ‘pwd’ command (print working directory) is used to display the current working directory. It’s a built-in command in certain Unix shells like ‘sh’ and ‘bash’ and can be easily implemented using POSIX C functions like ‘getcwd()’ and ‘getwd()’.

Users who are familiar with MS-DOS or the Windows command prompt may type ‘cd’ alone to view the working directory. However, in Linux and Unix, typing ‘cd’ alone simply returns you to the home directory.

The pwd command is considered as one of the most frequently used commands on Linux, AIX, HP-UX, *BSD, and other UNIX like operating systems along with the ls, and cd commands. It can be used for the following purposes under Apple OS X or UNIX or Linux operating systems:

  • Finding the full path to the current directory.
  • Storing the full path in a shell variable.
  • Verifying the absolute path.
  • Confirming the physical path, excluding symbolic links.

Syntax

 pwd [OPTION]...

pwd Command Options:

advertisement
advertisement
  • -L, –logical: Utilize the PWD from the environment, even if it includes symlinks.
  • -P, –physical: Avoid all symlinks.

pwd Command Examples

Example 1: Simple usage of pwd

To check the current directory, simply enter:

$ pwd
/usr/local/bin

Example 2: Displaying the Logical Current Working Directory

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

To display the logical current working directory, use:

$ pwd -L

The -L option makes pwd use $PWD from the environment, even if it contains symbolic links. It provides the absolute name of the current directory, possibly with symbolic links.

Example 3: Display the physical current working directory (all symbolic links resolved)

advertisement

Consider ~/bin/ as a symbolic link:

$ pwd
$ ls -l ~/bin/

Sample output:

lrwxrwxrwx 1 abc abc 35 May 13  2012 /home/abc/bin -> /home/abc/realdata/scripts/utils/

Now, navigate to ~/bin/ and confirm the current working directory with pwd:

advertisement
$ cd ~/bin/
$ pwd

Sample output:

/home/abc/bin

To view the actual physical current working directory and avoid symlink resolution, use:

$ pwd -P

Sample output:

/home/abc/realdata/scripts/utils

Example 4: To see the version of pwd command

To view the version information of the pwd command, use the following command:

$ pwd --version

Sample output:

pwd (GNU coreutils) 8.13
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
 
Written by Jim Meyering.

This command provides information about the version of the pwd command and its licensing details.

Example 5: Finding All Locations of the ‘pwd’ Executable

To locate all instances of the “pwd” executable, you can use the following command:

$ type -a pwd

This command will display information about all the locations where the “pwd” executable is stored.

Example 6: To get the help

To get help with the pwd command, use:

/bin/pwd --help

This displays usage information and options:

-L, --logical: Use PWD from the environment, even if it contains symlinks.
-P, --physical: Avoid all symlinks.
--help: Display help and exit.
--version: Output version information and exit.

Note: Please remember that your shell may have its own version of the pwd command, which might offer different options and functionality. To understand the specific capabilities of your shell’s pwd command, refer your shell’s documentation or run info coreutils pwd invocation in your terminal for comprehensive guidance.

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.