pwd command

When we work in terminal or command line, sometime we want to know in which current directory we are working. And sometimes we also want to know in which previous directory I worked.These requirement are simple but are very important for system administration.

Information of working directory is also required in scripting. When system administrator do troubleshooting, they also require the information of working directory.

To Print Current Working Directory

Using shell builtin command:

To print the working directory in Linux/Unix, we can use shell builtin command.
(Use type command to know which command type it is – Internal or External command)

pwd

Example: The pwd command output shows the absolute path i.e /home/linux

linux@tuxworld:~$ pwd
/home/linux
linux@tuxworld:~$

Using shell variable :

We can also use shell variable to find the current working directory.Use the below given shell variable

echo $PWD

Example : The output from command echo $PWD, shows path /root

root@tuxworld:~# echo $PWD
/root
root@tuxworld:~# 

To Print Previous Working Directory

To print Previous Working Directory, we will use shell variable command as given below

echo $OLDPWD

Example: In this example, the output of echo $OLDPWD shown the previous working directory. And the output of pwd command shows current working directory.

root@tuxworld:~# echo $OLDPWD
/var/www
root@tuxworld:~# 
root@tuxworld:~# pwd
/root
root@tuxworld:~#

Note: When we use cd command with option – (hyphen) means cd - command. It is similar to running the command cd $OLDPWD .

Read Some More Articles

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.