Using pwd command in Linux


Gaming Laptop Series

The pwd is a command in Linux which displays the current working directory of a user. So you might think what is the current working directory? while working in a Linux terminal you navigate among different directories in your system. Directories in Linux are organized in a tree structure under the / so if you use pwd command it displays the full path where you are currently in the directory tree.

The pwd command

Pwd stands for the print working directory when you use this command in your terminal it prints the working directory of a user. It is a built-in Linux command that stores the working path in the $PWD environment variable.

Syntax of the command-

pwd [option]

How to display the current working directory in Linux

While moving from one directory to another you can check where are you at any point of time using the pwd command. You can use the pwd command in one of the given forms –

We generally use pwd command without any argument as it is given below –

pwd

Or using option -L will print the working directory even if contains symbolic links

pwd -L

Using pwd with option -P will print the actual path without symlinks –

pwd -P

How to store the output of pwd command in a variable

Now let’s say you want to store the output of the pwd command in a variable called val then use the following command in your terminal –

val=$(pwd)

You can print the value inside val variable by using –

echo "The current working directory is: $val"

How to display the previous working directory

If you want to see the previous working directory then you can use the following command in your terminal –

echo "$OLDPWD"

You can see the manual page of pwd by using the following command in your terminal –

man pwd

Conclusion

Now you know how to check the current or previous working directory in your terminal. You can share your thought or ask queries in the comments below.

Leave a Comment

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