Categories: File Management

Pwd command on Linux (current directory) [Basic Guide]

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

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Recent Posts

Sudo command on Linux (privilege scale) [Basic Guide]

The sudo command on Linux executes a given command as if it were another user.…

2 years ago

SS command on Linux (investigate the network) [Basic Guide]

The ss command on Linux is extremely useful for investigating sockets, providing various information about…

2 years ago

Free Linux command (memory usage) [Basic Guide]

Free Linux command shows the amount of total memory in use and available, as well…

2 years ago

Linux while command (loop – while) [Basic Guide]

The shell has structures for testing conditions and executing certain program sequences several times (loop),…

2 years ago

Linux fstab file (disk mount setup) [Basic Guide]

The /etc/fstab file stores the configuration of which devices should be mounted and what is…

2 years ago

Netcat command on Linux (Swiss network knife) [Basic Guide]

The Netcat Command in Linux or nc is a utility used to do “almost anything”…

2 years ago

This website uses cookies.