Skip to main content

12 Different Ways To Use pwd command

NAME
pwd - print name of current/working directory.
It prints the path of the working directory starting from root(/). 

SYNOPSIS
pwd [option]...

DESCRIPTION
Print the full filename of the current working directory.
It has 2 flags and it takes no arguments.
-L, --logical
use pwd from environment, even if it contains symbolic links.

-P, --physical
avoid all symbolic links.

If both -L and -P options are used, -L is taken into priority. And if no option is specified at the command prompt, pwd will avoid all symlinks, so -P is assumed.


--help 
display this help and exit

--version
output version information and exit

Returns zero unless an invalid option is given or the current directory cannot be read.
Therefore, 0 = success.
Non-zero(any character that is not zero) = failure.

NOTE: your shell may have its own version of pwd, which usually supersedes the version described here. So we'll use /bin/pwd instead of pwd alone.
Reason:
pwd alone means the shell built-in pwd and your shell may have a different version of pwd.
When we use /bin/pwd we are calling the binary version of that command. 
Both versions of the command print the current working directory though the binary version has many options.
Please refer to your shell's documentation for details about the options it supports.

1. Print your current working directory
 /bin/pwd  

2. Create a symbolic link of a folder. e.g /var/www/html as htm. Move to the newly created directory and print working directory with and without symbolic links.
 ln -s /var/www/html/ htm  

 cd htm

3. Print working directory from environment even if it has symbolic links:
 /bin/pwd -L  

4. Print actual physical working directory without any symbolic links:
 /bin/pwd -P  

To delete or remove symbolic links, you need to have write permissions on the directory that contains the symlink. When you remove a symlink, the file it points to is not affected.
To delete a symlink, invoke the rm command followed by the symlink name.
5. Print version of your pwd command:
 /bin/pwd --version  

6. Print all the locations containing executable named pwd.
 type -a pwd  

7. Store the value of pwd command in a variable, (x) and print its value from the variable. This is important for shell scripting.
Alternatively we can use printf
 x=$(pwd)  

 echo "Current working directory is : $x"  

8. Check the previous and current working directory at the same time.
 echo "$PWD $OLDPWD"  

9. The absolute path starting from root(/) of the pwd binary file.
 /bin/pwd  

10. The absolute path starting from root(/) of the pwd source file.
 /usr/include/pwd.h  

11. The absolute path starting from root(/) of the pwd manual pages file.
 /usr/share/man/man1/pwd.1.gz  

12. Write a shell script that analyses your current working directory in your home directory. If you are under directory A it will output "You are in directory A" else create directory A under your home directory and ask you to cd into it.

First create directory A and under it create a shell script file with name pwd.sh
 mkdir eneri  
 cd eneri  
 sudo nano pwd.sh  
Now add the following script to the pwd.sh file.
 #!/bin/bash  
 x="$(pwd)"  
 if [ "$x" == "/home/$USER/eneri" ]  
 then  
 {  
   echo "You are now in eneri directory"  
 }  
 else  
 {  
   mkdir /home/$USER/eneri  
   echo "Created Directory eneri you may now cd into it"  
 }  
 fi  
Give execute permission and run it.
 sudo chmod 755 pwd.sh 
 
./pwd.sh  

One important concept to understand is that the shell has a notion of a default location in which any file operations will take place. This is its working directory. 

If you try to create new files or directories, view existing files, or even delete them, the shell will assume you’re looking for them in the current working directory unless you take steps to specify otherwise. 

So it’s quite important to keep an idea of what directory the shell is “in” at any given time, after all, deleting files from the wrong directory could be disastrous. 

If you’re ever in any doubt, the pwd command will tell you exactly what the current working directory is.

Comments

Post a Comment

Your input is valued. Please type something....