Skip to content

Pwd command on Linux (current directory)

The pwd command reports the current absolute directory. Absolutely it is understood that it shows the complete path from the root of the system.

It has a few options:

  • -L or —logical Lists the contents of the PWD variable, even if it contains symbolic links
  • -P or —physical Avoid all symbolic links

In this example, the /home/program directory is the symbolic link for /home/software.

The pwd command without an option or with the “-L” option, the symbolic link is indicated.

With the “-P” option, pwd resolves the symbolic link pointing to the original directory.

$ ls -l lrwxrwxrwx 1 root 8 Sep 12 13:16 program -> software drwxr-xr-x 7 Apache users 4096 Feb 1 2013 software
$ cd /home/program
$ pwd /home/program
$ pwd -L /home/program
$ pwd -P /home/software

The pwd is a command that can be widely used by programmers to check which current directory you are working in.

However, to make it even easier, this information can be placed on the command line by changing the shell’s PS1 variable:

$ PS1= "[ u@ h W] $"

This line will produce a shell prompt like this:

[uira @maquina learnlinux] $

By changing the “W” to “w”, you can obtain the full path of the current directory:

$ PS1= "[ u@ h w] $"

This way, we have the prompt:

[uira @maquina /home/learnlinux] $

To make this change permanent, you can add this line to the .bashrc file in the user’s HOME directory:

echo 'PS1= "[ u@ h w] $ “'>> ~/.bashrc

Go to and back in Directories with the minus sign “-”

Another very useful shortcut for programmers are the exchange of directories between the current and the last directory, using the “-” option of the cd command:

[uira @maquina /home/learnlinux] $ cd /var/log/nginx [uira @maquina /var/log/nginx] $ cd - /home/learnlinux [uira @maquina /home/learnlinux] $ cd - /var/log/nginx [uira @maquina /var/log/nginx] $

Note that the “cd -” can make you go back and forth from one directory to another, saving you a lot of fingers, especially when you want to go back and forth from the application directory, and the logs directory, as in the example.

Home directory with the tilde sign “~”

The shortcut with the “~” sign can be used to reference the logged-in user’s home directory:

$ cd ~
$ pwd
/home/uira