For those working with Linux command Line, command ‘pwd‘ is very helpful, which tells where you are – in which directory, starting from the root (/). Specially for Linux newbies, who may get lost amidst of directories in command Line Interface while navigation, command ‘pwd‘ comes to rescue.
What is pwd?
‘pwd‘ stands for ‘Print Working Directory‘. As the name states, command ‘pwd‘ prints the current working directory or simply the directory user is, at present. It prints the current directory name with the complete path starting from root (/). This command is built in shell command and is available on most of the shell – bash, Bourne shell, ksh,zsh, etc.
Basic syntax of pwd:
# pwd [OPTION]
Options used with pwd
Options | Description |
-L (logical) | Use PWD from environment, even if it contains symbolic links |
-P (physical) | Avoid all symbolic links |
–help | Display this help and exit |
–version | Output version information and exit |
If both ‘-L‘ and ‘-P‘ options are used, option ‘L‘ is taken into priority. If no option is specified at the prompt, pwd will avoid all symlinks, i.e., take option ‘-P‘ into account.
Exit status of command pwd:
0 | Success |
Non-zero | Failure |
This article aims at providing you a deep insight of Linux command ‘pwd‘ with practical examples.
1. Print your current working directory.
avi@tecmint:~$ /bin/pwd /home/avi
2. Create a symbolic link of a folder (say /var/www/html into your home directory as htm). Move to the newly created directory and print working directory with symbolic links and without symbolic links.
Create a symbolic link of folder /var/www/html as htm in your home directory and move to it.
avi@tecmint:~$ ln -s /var/www/html/ htm avi@tecmint:~$ cd htm
3. Print working directory from environment even if it contains symlinks.
avi@tecmint:~$ /bin/pwd -L /home/avi/htm
4. Print actual physical current working directory by resolving all symbolic links.
avi@tecmint:~$ /bin/pwd -P /var/www/html
5. Check if the output of command “pwd” and “pwd -P” are same or not i.e., if no options are given at run-time does “pwd” takes option -P into account or not, automatically.
avi@tecmint:~$ /bin/pwd /var/www/html
Result: It’s clear from the above output of example 4 and 5 (both result are same) thus, when no options are specified with command “pwd”, it automatically takes option “-P” into account.
6. Print version of your ‘pwd’ command.
avi@tecmint:~$ /bin/pwd --version pwd (GNU coreutils) 8.23 Copyright (C) 2014 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. 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.
Note: A ‘pwd’ command is often used without options and never used with arguments.
Important: You might have noticed that we are executing the above command as “/bin/pwd” and not “pwd”.
So what’s the difference? Well “pwd” alone means shell built-in pwd. Your shell may have different version of pwd. Please refer manual. When we are using /bin/pwd, we are calling the binary version of that command. Both the shell and the binary version of command Prints Current Working Directory, though the binary version have more options.
7. Print all the locations containing executable named pwd.
avi@tecmint:~$ type -a pwd pwd is a shell builtin pwd is /bin/pwd
8. Store the value of “pwd” command in variable (say a), and print its value from the variable (important for shell scripting perspective).
avi@tecmint:~$ a=$(pwd) avi@tecmint:~$ echo "Current working directory is : $a" Current working directory is : /home/avi
Alternatively, we can use printf, in the above example.
9. Change current working directory to anything (say /home) and display it in command line prompt. Execute a command (say ‘ls‘) to verify is everything is OK.
avi@tecmint:~$ cd /home avi@tecmint:~$ PS1='$pwd> ' [Notice single quotes in the example] > ls
10. Set multi-line command line prompt (say something like below).
/home 123#Hello#!
And then execute a command (say ls) to check is everything is OK.
avi@tecmint:~$ PS1=' > $PWD $ 123#Hello#! $ ' /home 123#Hello#!
11. Check the current working directory and previous working directory in one GO!
avi@tecmint:~$ echo “$PWD $OLDPWD” /home /home/avi
12. What is the absolute path (starting from /) of the pwd binary file.
/bin/pwd
13. What is the absolute path (starting from /) of the pwd source file.
/usr/include/pwd.h
14. Print the absolute path (starting from /) of the pwd manual pages file.
/usr/share/man/man1/pwd.1.gz
15. Write a shell script analyses current directory (say tecmint) in your home directory. If you are under directory tecmint it output “Well! You are in tecmint directory” and then print “Good Bye” else create a directory tecmint under your home directory and ask you to cd to it.
Let’s first create a ‘tecmint’ directory, under it create a following shell script file with name ‘pwd.sh’.
avi@tecmint:~$ mkdir tecmint avi@tecmint:~$ cd tecmint avi@tecmint:~$ nano pwd.sh
Next, add the following script to the pwd.sh file.
#!/bin/bash x="$(pwd)" if [ "$x" == "/home/$USER/tecmint" ] then { echo "Well you are in tecmint directory" echo "Good Bye" } else { mkdir /home/$USER/tecmint echo "Created Directory tecmint you may now cd to it" } fi
Give execute permission and run it.
avi@tecmint:~$ chmod 755 pwd.sh avi@tecmint:~$ ./pwd.sh Well you are in tecmint directory Good Bye
Conclusion
pwd is one of the simplest yet most popular and most widely used command. A good command over pwd is basic to use Linux terminal. That’s all for now. I’ll be here again with another interesting article soon, till then stay tuned and connected to Tecmint.
What is the name of the file contained in this directory?
/etc/apache2/sites-available/000-default.conf
What is the directory that is the DocumentRoot for your website?
What is the path of the current working directory just after you logged in?
@Kiya,
Your home directory /home/user or /root.
Thank you for this wonderful article…
I want to print the filenames in my current directory.
“pwd” doesn’t seem to print anything.
what do I do to make it actually print the working directory?
@David,
Do you mean you want to list filenames? then use ls -l command. The pwd only print the current/present working directory location…I hope you get it now.
Write a shell script for printing all the files related information pwd( eg:size ,permissions
@prasad,
Do you want me to write the script or want help on the script you are writing.
Great tutorial! Above, you make the following comment:
“If no option is specified at the prompt, pwd will avoid all symlinks, i.e., take option ‘-P‘ into account.”
However, in Fedora 20, pwd without options defaults to pwd -L.
Dear teancum144,
I would surely like to go through my Fedora 20 Local server to confirm. This is quiet possible. Thanks for your feedback.
Your pwd article could contain so much.
Thanks for a good article.
Thanks for the appreciation @ Minecraft.
Keep Connected!
A few corrections…
If -L and -P are both used, -L does not have priority; they each override the other (last one wins). Also, for coreutils pwd the default depends on an environment setting. From “info coreutils”:
“If `-L’ and `-P’ are both given, the last one takes precedence. If
neither option is given, then this implementation uses `-P’ as the
default unless the `POSIXLY_CORRECT’ environment variable is set.”
As far as I know the shell built-in versions of pwd in all the POSIX-type shells (bash, dash, ksh, etc.) follow the POSIX requirement of defaulting to -L.
The /usr/include/pwd.h header is unrelated – it provides the API for accessing /etc/passwd (or network equivalents).
Yeah Geoff!
avi@tecmint:~/htm$ /bin/pwd
/var/www/html
avi@tecmint:~/htm$ /bin/pwd -L
/home/avi/htm
avi@tecmint:~/htm$ /bin/pwd -P
/var/www/html
avi@tecmint:~/htm$ /bin/pwd -P -L
/home/avi/htm
avi@tecmint:~/htm$ /bin/pwd -L -P
/var/www/html
we need to correct the write-up. Thanks for pointing that out.
Never thought your pwd article could contain so much – especially liked the tie in with the “type” command – which I should have known but did not.
Thanks for a surprisingly good article.
Welcome djf!
Surprisingly good article means?
were you expecting a bad article?