“pwd” command in Linux/Unix

The "pwd" command in Linux and Unix is a command-line tool that is used to display the current working directory.

Usage

The "pwd" command stands for "print working directory." The basic syntax for the "pwd" command is as follows:

pwd

Displaying the Current Working Directory

The most basic use of the "pwd" command is to display the current working directory. When run, the "pwd" command will display the full path of the current directory that you are in. For example, if you are currently in the "/home" directory, running the command "pwd" would display "/home" as the output.

pwd

Verbose Option

The "pwd" command also has a "verbose" option, "-P," which displays the physical directory, which could be different from the logical directory. This option is useful when working with symbolic links, it will show the real path instead of the symlink path.

pwd -P

Using “pwd” in Combination with Other Command-line Tools

The "pwd" command can also be used in combination with other command-line tools, such as "grep" and "awk," to filter and process the output. For example, you can use "grep" to search for a specific string within the output of the “pwd” command.

pwd | grep "home"

Another example is using "awk" to format the output of the "pwd" command in a way that makes it easier to read or analyze.

pwd | awk '{print $1}'

Changing the Current Working Directory

The "cd" command is used to change the current working directory. The basic syntax of the "cd" command is as follows:

Advertisements
cd [directory]

For example, if you want to change the current working directory to the "documents" directory, you would use the command:

cd ~/documents

The "cd" command can also be used with the "." and ".." to navigate through directories. The "." represents the current directory and ".." represents the parent directory. For example, if you are currently in the "/home/documents" directory and you want to navigate to the parent directory, you would use the command:

cd ..

This would take you to the "/home" directory. Similarly, if you want to navigate to a subdirectory within the current directory, you can use the "." with the subdirectory name.

cd ./subdirectory

Using "cd" with the "~" symbol will take you to your home directory.

cd ~

Conclusion

In conclusion, the "pwd" command is a simple yet useful command-line tool that can be used to display the current working directory in Linux and Unix. It has a verbose option that can be used to display the physical path of the directory. It can also be used in combination with other command-line tools to filter and process the output. Understanding how to use the "pwd" command effectively can greatly improve your productivity when working in a Linux or Unix environment. Together with the "cd" command, it forms a fundamental pair of commands for managing the current directory and navigating the file system in Linux/Unix systems.

Advertisements

LEAVE A REPLY

Please enter your comment!
Please enter your name here