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.
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.
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.
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
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.
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.
#!/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.
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.
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.
Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…
Introduction A self-signed SSL certificate is a certificate that is created and signed by the…
Introduction Debugging is an important part of Bash scripting. When a script does not work…
Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…
Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…
Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…