Absolute and Relative Paths
Absolute and Relative Paths
When navigating the Linux filesystem, it's important to understand the difference between absolute and relative paths.
Absolute Paths
An absolute path refers to a file or directory's location in relation to the root directory /
. This means that the absolute path always starts with /
and includes every directory down to the target file or directory.
For example, the absolute path to the /etc
directory is:
And the absolute path to the passwd
file in the /etc
directory is:
Relative Paths
A relative path refers to a file or directory's location in relation to the current working directory. This means that the relative path does not start with /
and only includes the directories needed to reach the target file or directory.
For example, if your current working directory is /home/user
, the relative path to the Desktop
directory in your home directory would be:
And the relative path to the passwd
file in the /etc
directory would be:
Notice that the relative path to the passwd file includes two .. directories to move up the directory hierarchy to the root directory, and then back down to the etc directory.
Using Paths in Commands
When using paths in commands, you can use either absolute or relative paths. It's important to use the correct path, as using an incorrect path can result in errors or unintended consequences.
For example, the ls
command can be used with either an absolute or relative path:
Both commands will list the files and directories in the specified directories, but the second command uses a relative path to move up two directories before reaching the var
directory.
Overall, understanding how to use absolute and relative paths is essential for navigating the Linux filesystem and using commands effectively.