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

Install Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from…

1 day ago

Install R on Ubuntu 18.04 from CRAN: Statistical Computing Setup

R is an open-source programming language and environment built for statistical computing and data visualization. It…

1 day ago

Install Jenkins on Ubuntu 18.04: CI/CD Server Setup Guide

Jenkins is an open-source automation server that makes it easy to build CI/CD pipelines. Continuous integration…

1 day ago

Install Android Studio on Ubuntu 18.04 with Snap and OpenJDK 8

Android Studio is the official IDE for Android development, built on JetBrains' IntelliJ IDEA platform. It…

1 day ago

Install and Configure GitLab on Ubuntu 18.04 with Omnibus

GitLab is a web-based, open-source Git repository manager written in Ruby. It includes built-in tools for…

1 day ago

Install Anaconda on Ubuntu 18.04: Python Data Science Setup Guide

Anaconda is the most widely used Python distribution for data science and machine learning. It bundles…

2 days ago