Linux pwd Command Tutorial

Linux and Unix operating systems provide the pwd command which prints the full pathname of the current working directory. The pwd name comes from print working directory . As a simple command is provided few options. the pwd command is provided as a shell built-in command for most of the shells like Bourne shell, bash, ash, ksh, zsh etc. Also the pwd command is provided by all of the Linux and Unix distributions like Ubuntu, Debian, CentOS, RHEL, Mint, Kali, etc.

What Does the pwd Command Do?

You may as the question “What does the pwd command do?”. Simply the pwd command prints the current working directory. It is very useful to decide the current working directory if it is not displayed int the shell or alternatively used to get the current working directory in bash or other scripts to work properly.

Print Current Working Directory

The current working directory can be printed with the pwd command without any option. As stated previously the pwd command is the full path of the current working directory. But if there is a symlink during directory change the symlink is printed

$ pwd
/home/ismail/symlink/backup

Display pwd Command Options

The --help option can be used to display pwd command options. This lists the option and a brief explanation of the command.

$ pwd --help
Display pwd Command Options

Print Current Working Directory Avoiding Symlinks

By default the pwd command prints the current working directory with symlinks. But we can prevent the symlinks and print real directories with the -P option like below.

$ pwd -P

Using pwd Command In Bash Script

One of the popular use cases for the pwd command is getting the current working directory in bash scripts. As the script can be called from different paths or during script execution the working directory can be changed we can use the pwd command to get the current working directory. In the following bash script, we print the current working directory and assign it into the variable named p .

#!/bin/bash

echo $(pwd)

p = $(pwd)

Leave a Comment