How To

pwd Command In Linux: Print Current Working Directory

Introduction

The pwd command in Linux is used to print the current working directory. It is one of the most basic and useful commands for beginners learning Linux and Bash scripting. When you work in the terminal, you often move between different folders using the cd command. The pwd command helps you know exactly where you are in the Linux file system.

If you are learning Bash scripting, understanding pwd is important because many scripts depend on directory paths. You may need to know the current location before creating files, saving logs, running tools, or managing backups. The pwd command is also useful in cybersecurity workflows when working with scripts, scan results, reports, and tool directories.

What Is The pwd Command In Linux?

The word pwd stands for print working directory. It displays the full path of the directory you are currently working in.

Basic syntax:

pwd

Example output:

/home/kali

This means your current directory is /home/kali.

Basic pwd Command Example

Open your terminal and run:

pwd

You may see output like:

/home/user

Now move to another directory:

cd /tmp

Run pwd again:

pwd

Output:

/tmp

This shows that your current working directory has changed.

Using pwd In Bash Scripts

The pwd command is often used inside Bash scripts to show or store the current directory.

Create a script:

nano pwd-example.sh

Add the following code:

#!/bin/bashecho "Current working directory:"pwd

Save and run the script:

chmod +x pwd-example.sh./pwd-example.sh

Output:

Current working directory:/home/kali

Store pwd Output In A Variable

You can store the output of pwd in a Bash variable.

#!/bin/bashcurrent_dir=$(pwd)echo "You are currently in: $current_dir"

This is useful when you want to reuse the current directory path later in the script.

Create A File In The Current Directory

You can use pwd to confirm where a file will be created.

#!/bin/bashecho "Current directory is: $(pwd)"echo "Creating report file..."touch report.txtecho "File created in: $(pwd)"

This helps avoid confusion when scripts create files in unexpected locations.

Cybersecurity Example: Save Scan Results In Current Directory

#!/bin/bashscan_dir=$(pwd)target="192.168.1.1"echo "Saving scan result in: $scan_dir"nmap "$target" > "$scan_dir/nmap-result.txt"echo "Scan completed. Result saved as nmap-result.txt"

This script stores an Nmap scan result in the current working directory.

Difference Between pwd And $PWD

Linux also provides a shell variable called $PWD.

Example:

echo $PWD

This usually shows the same result as:

pwd

Both are useful, but pwd is a command, while $PWD is an environment variable.

Conclusion

The pwd command in Linux is simple but very important. It shows the current working directory and helps users understand their location in the file system.

For Bash scripting beginners, pwd is useful for working with paths, creating files, saving reports, and avoiding directory mistakes. Whether you are managing files, writing automation scripts, or saving cybersecurity scan results, the pwd command is a basic Linux command you should know.

Cyber Defence

Recent Posts

Bash Scripting Best Practices Every Beginner Should Know

Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…

57 minutes ago

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

2 hours ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

6 hours ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

7 hours ago

How To Use Pipes In Bash Scripts For Command Chaining

Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…

8 hours ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

9 hours ago