It is very important and a necessary thing to do to look around before executing something especially in a Command Line Interface-based operating system such as Linux or Unix variant distribution.
It is because a slight mistake such as failing to identify where is currently the working directory are will have a different impact or will have the wrong outcome to the one which is expected.
For example running ‘rm -rf *’ will result on erasing all of the content within the currently working directory. Executing this command in a different directory which is being targeted will end in erasing the wrong files or folders exists within the current working directory.
So, in order to make sure whether or not the command is executed in the right location, type the following command to make sure that the currently working directory is really the one which is being targeted by the command :
pwd
The execution of the command in real bash prompt :
user@soulreaper:~/test-folder$ pwd /home/user/test-folder user@hostname:~$
As we can see part of the bash prompt shown some part of current working directory which is ‘/test-folder’. But how about the bash prompt shown below :
user@hostname:~$ pwd /home/user user@hostname:~$
It doesn’t even show any meaningful information except ~ which is a sign means that the current working directory is now in the currenly logged in user’s home directory which in this case is in the /home/user.
It is very important to be able to recognize the full path of where we are now working with the help of ‘pwd’ command because sometime the bash prompt doesn’t show any valuable information at all as shown with the following bash prompt :
$
But by executing the command, the real information regarding current working directory can be viewed :
$ pwd /home/user/test-folder $
This is caused by the environment variable represented with PS1 is holding the information on how bash prompt should be viewed. Let’s look the current one which is showing the above bash prompt type :
$ echo $PS1 $ $
Compare it with the previous one which shown the full information :
user@hostname:~$ echo $PS1 \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ user@hostname:~$
So, aside from depending to read the information shown in the bash prompt which can be misleading or even nothing ever shown, use the command ‘pwd’ to check the currently working directory.
3 thoughts on “Check current working directory in Linux with pwd command”