Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


LPI Linux Essentials Exam 010-160 - Topic 2.3 - Absolute & Relative Paths

In the world of Linux, navigating the file system is an essential skill. You often need to specify the location of files or directories, and this is done through paths. There are two main types of paths you need to be familiar with: absolute and relative paths. Additionally, we'll explore the special relative paths . and .. that are crucial for efficient navigation.

Absolute Paths

An absolute path specifies the precise location of a file or directory from the root directory, denoted by /. It provides an unambiguous reference starting from the root of the file system. Here's how an absolute path looks:

/home/user/documents/myfile.txt
  • The leading / indicates the root directory.
  • home is a directory inside the root.
  • user is a subdirectory within home.
  • documents is a subdirectory within user.
  • Finally, myfile.txt is the file inside the documents directory.

When you use an absolute path, you can access the target file or directory from any location in the file system. This makes it an excellent choice when you need to refer to a specific file, regardless of your current directory.

Relative Paths

Relative paths, on the other hand, describe the location of a file or directory concerning the current working directory. They do not begin with / and instead use references like . and .. to navigate the file system.

The . (dot) Relative Path

The . (dot) represents the current working directory. It's handy when you want to specify a path relative to your current location. For example, if you are in /home/user/documents and you want to reference a file in the same directory, you can use:

./myfile.txt

Here, . stands for the current directory (/home/user/documents), and myfile.txt is a file in that directory. This allows you to access files within your current directory without needing to specify the full path.

The .. (dot-dot) Relative Path

The .. (dot-dot) represents the parent directory of the current working directory. This is useful when you want to navigate to a directory above your current location. For example, if you are in /home/user/documents and you want to access a file in the user directory, you can use:

../myfile.txt

In this case, .. refers to the parent directory of /home/user/documents, which is /home/user. So ../myfile.txt resolves to /home/user/myfile.txt.

Exercise

The following exercise will get you using the cd command to navigate directories using absolute and relative paths:

Exercise: Navigate the file system using an absolute and relative path:

Navigate to the /usr/share/doc directory using the absolute path:

cd /usr/share/doc

Navigate to the /usr/bin directory using the relative path:

cd ../../bin

Confirm your current directory using the print working directory command:

pwd

The command will return the directory you are in which will be:

/usr/bin

Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.