5 Practical pwd Command in Linux with Examples

«
»

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

“pwd” command is used T. This post describes “pwd” command used in Linux along with usage examples and/or output.

Description:

In Unix-like and some other operating systems, the pwd command (print working directory) is used to output the path of the current working directory.

The command is a shell builtin in certain Unix shells such as sh, and bash. It can be implemented easily with the POSIX C functions getcwd() and/or getwd().

Users who are familiar with MS-DOS or the Windows command prompt may type cd alone to print the working directory. However, typing cd alone in Linux and Unix will return you to the home directory.

Synopsis:

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

pwd [OPTION]…

OPTIONS:

-L
Logical Links

-P
Physical Links

Examples:

1. To print name of current/working directory(pwd):

sanfoundry-> pwd
/home/himanshu
sanfoundry-> cd SAN/
sanfoundry-> pwd
/home/himanshu/SAN
sanfoundry-> /bin/pwd
/home/himanshu/SAN

2. To show the full path when symlinks are there(pwd OR pwd -L):

sanfoundry-> ln -s folder/ linked_folder
sanfoundry-> ls -l linked_folder
lrwxrwxrwx 1 himanshu himanshu 7 Jul 28 17:15 linked_folder -> folder/
sanfoundry-> cd linked_folder
sanfoundry-> pwd 
/home/himanshu/SAN/linked_folder
sanfoundry-> pwd -L
/home/himanshu/SAN/linked_folder

Here “-L” stands for “–logical” and we can see that whole path upto the linked folder is shown with “-L” option.
But if you want to see the path to the original folder for the linked folder use “-P” option.

advertisement

3. To see the original folder of the linked folder(pwd -P):

sanfoundry-> ln -s folder/ linked_folder
sanfoundry-> ls -l linked_folder
lrwxrwxrwx 1 himanshu himanshu 7 Jul 28 17:15 linked_folder -> folder/
sanfoundry-> cd linked_folder
sanfoundry-> pwd 
/home/himanshu/SAN/linked_folder
sanfoundry-> pwd -P
/home/himanshu/SAN/folder

Here “-P” stands for “–physical” ans we can see that the path upto the folder directory is shown instead to the path of the linked directory linked_folder.

Note:Shell pwd vs /bin/pwd:
To use the binary version, type full path /bin/pwd:

advertisement
sanfoundry-> /bin/pwd 
/home/himanshu/SAN

By typing pwd, you end up using the shell builtin provided by bash or ksh:

pwd

4. To see all locations containing an executable named pwd:

sanfoundry-> type -a pwd
pwd is a shell builtin
pwd is /bin/pwd

The /bin/pwd version of pwd command has a two more additional options.

5. To see the version of pwd command:

sanfoundry-> /bin/pwd  --version 
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.

6. To get the help:

sanfoundry-> /bin/pwd --help 
Usage: /bin/pwd [OPTION]...
Print the full filename of the current working directory.
 
  -L, --logical   use PWD from environment, even if it contains symlinks
  -P, --physical  avoid all symlinks
      --help     display this help and exit
      --version  output version information and exit
 
NOTE: your shell may have its own version of pwd, which usually supersedes the version described here.  Please refer to your shells documentation for details about the options it supports.
 
Report pwd bugs to bug-coreutils@gnu.org
GNU coreutils home page: 
General help using GNU software: 
For complete documentation, run: info coreutils pwd invocation'

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.

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 & technical discussions at Telegram SanfoundryClasses.