pwd Command in Linux: Display Current Working Directory in Terminal
The pwd command shows the current absolute directory. By absolute, it means it shows the full path from the root of the system.
It has some options:
- -L or --logical Displays the content of the PWD variable, even if it contains symbolic links
- -P or --physical Resolves all symbolic links
In this example, the /home/programa directory is a symbolic link to /home/software.
The pwd command without options or with the "-L" option shows the symbolic link.
With the "-P" option, pwd resolves the symbolic link pointing to the original directory.
$ ls -llrwxrwxrwx 1 root root 8 set 12 13:16 programa -> softwaredrwxr-xr-x 7 apache users 4096 fev 1 2013 software
$ cd /home/programa
$ pwd/home/programa
$ pwd -L/home/programa
$ pwd -P/home/software
The pwd command is very useful for programmers to verify which current directory they are working in.
However, to make it even easier, this information can be placed on the command line by modifying 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 get 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 user's HOME directory .bashrc file:
echo 'PS1="[\u@\h \w]\$ "' >> ~/.bashrc
Going Back and Forth Between Directories with the Minus Sign "-"
Another very useful shortcut for programmers is switching between 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 "cd -" can take you back and forth between directories, saving your fingers a lot of work, especially when you want to go back and forth from the application directory and the logs directory, as shown in the example.
The Home Directory with the Tilde "~"
The tilde "~" shortcut can be used to reference the logged-in user's home directory:
$ cd ~
$ pwd
/home/uira
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, simply log in or create your user here.
Like it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.