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 is)
Example: The pwd command output shows the absolute path i.e /home/linux
[email protected]:~$ pwd /home/linux [email protected]:~$
Using shell variable :
We can also use shell variable to find the current working directory.Use the below given shell variable
Example : The output from command
echo $PWD, shows path /root
[email protected]:~# echo $PWD /root [email protected]:~#
To Print Previous Working Directory
To print Previous Working Directory, we will use shell variable command as given below
Example: In this example, the output of
echo $OLDPWD shown the previous working directory. And the output of
pwd command shows current working directory.
[email protected]:~# echo $OLDPWD /var/www [email protected]:~# [email protected]:~# pwd /root [email protected]:~#
Note: When we use cd command with option – (hyphen) means
cd - command. It is similar to running the command