Technical skill

Linux command: pwd

Subject: .../LINUX

Find out which folder commands are using

Every shell has a current working directory. A relative path starts from that folder, so a command can read or create a file in a different place than expected if the shell is somewhere else. pwd prints the current directory as an absolute path. It does not move the shell or print the home directory unless that happens to be the current location.

If pwd prints /srv/app, a command such as cat config.yml looks for /srv/app/config.yml. An absolute path starts at the root directory, the top of the filesystem tree, as in /etc/ssh/sshd_config. cd changes the shell's current directory, and commands started afterward use the new location. Check it before using a broad relative path, especially before moving, replacing, or removing files.

Paths that pass through a link

A symbolic link is a path that points to another file or directory. If /var/log/myapp is a symbolic link, the name you use and the physical location it leads to may differ. PWD is a shell variable that normally stores the logical path to the current directory. pwd -L uses that path when the value is valid. pwd -P resolves symbolic links and prints the physical path. Keep the logical name when it is the useful name in a workflow; use the physical form when the resolved location matters.

The shell commonly provides pwd as a builtin, and GNU Coreutils also provides a separate utility. The builtin can use directory information the shell maintains. An external utility obtains the path through the process environment and system interfaces. In Bash, type -a pwd shows which definitions are available. Use pwd -P in a portable script when the physical path is explicitly required.

Save the path for later commands

Command substitution captures command output in a variable:

project_dir=$(pwd -P)
config_file="$project_dir/config/app.yml"
printf 'Reading: %s\n' "$config_file"

Quoting the variable keeps spaces and wildcard characters in the path from being split or expanded. If a script already knows which folder it should use, taking that directory as an argument can be clearer than relying on where the caller started it.

PWD is useful in an interactive shell, but a program should not treat an arbitrary environment value as proof of the real filesystem location. pwd -P asks the shell or utility for the physical location. A path can change if a directory is renamed or mounts change, so it names a location within a filesystem view rather than a permanent identifier.

Interactive exercises

Materials

Skillsets
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Skyeng QA Automation Hard Skillset v09.21 preview
image descriptionSkyeng QA Automation Hard Skillset v09.21
by Aleksandra Kulikova
16 users
Skillset Skyeng SRE preview
image descriptionSkyeng SRE
by Yuliya Marochkina
12 users
Show more
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful