Kiến Thức Linux

Absolute Path vs. Relative Path Explained

Absolute path vs. relative path explained.

Introduction

Absolute and relative paths describe the location of a file or directory within a file system. They represent a set of directions for the OS to find a specific item. Both casual and advanced users benefit from understanding how to use paths.

In this article, you will learn the difference between an absolute and relative path and see how to find and create them in your system.

Absolute path vs. relative path explained.Absolute path vs. relative path explained.

What Are Paths?

Paths in a computer system refer to the hierarchical way of specifying the location of a file or directory. Paths are crucial for the operating system as they enable it to locate and access files and directories.

There are two types of paths:

  • Absolute path. The complete, unambiguous route to a file or directory from the system root.
  • Relative path. The location of a file or directory in relation to the current working directory.

The choice between the paths depends on the specific task and the need for portability in the given situation. The sections below explain the two path types in detail and provide examples for each.

What Is an Absolute Path?

An absolute path is the location of a file or directory specified from the root directory. It represents the complete path from the start of the filesystem from the root directory.

The absolute path always begins from the start of the hard drive or other storage device, describing every step the user must take through the file system to end up in the specified location. Absolute paths are unambiguous and easy to understand, and they are useful when you need to specify a file or directory’s location precisely, irrespective of your current working directory.

However, absolute paths can also be inconvenient to work with, especially when working with deeply nested directories.

Absolute Path Example

Depending on the OS type, an absolute path is presented differently. The start of the absolute path is the root directory. It is represented as a forward slash (/) in Unix-like operating systems or as a drive letter and backslash (e.g., C:) in Windows.

An example of an absolute path in Linux is:

/home/user/documents/file.txt

On the other hand, an absolute path in Windows would be:

C:UsersUserDocumentsfile.txt

Both paths start from the beginning of the drive or root directory and lead up to the file.txt file. However, each OS uses a different way of specifying the path.

What Is a Relative Path?

A relative path is the location of a file or directory relative to your current working directory. Unlike an absolute path, the relative path doesn’t start with a slash or drive letter since it doesn’t start from the root directory.

Relative paths simplify working with files by minimizing the typing needed to specify the file or directory path. A relative path is used whenever you refer to a file or directory only by its name. That is the simplest form of a relative path, and the shell looks for the specified file name within the current directory.

Relative paths adapt to changes in the current working directory, making them convenient for scripting, development, and file management within a localized context.

This path type also accepts entry points, allowing users to specify relative paths for files closer to the root directory. For example:

  • The .. entry represents the parent directory.
  • The . entry represents the current directory.

For anything closer to root, use the absolute path.

Relative Path Example

The relative path is specified differently in Linux and Windows, as the two OSes use / and to separate directories, respectively.

Linux

When a current working directory is /home/user/documents, and you want to reference a file named file.txt in the same directory, use the following relative path:

file.txt

The system looks for the specified file in the current working directory.

Similarly, to reference a file named report.txt in a subdirectory called reports within the current directory, use a relative path as follows:

./reports/report.txt

The path tells the OS to look for the report.txt file within the reports directory in the current working directory.

Windows

When you are currently located in the directory C:UsersJohnDocuments, and you want to reference a file named document.txt in the current directory, use the following relative path:

.document.txt

This path instructs Windows to look for the document.txt file in the current working directory, which is C:UsersJohnDocuments. The dot (.) represents the current directory.

If you want to specify a file named invoice.pdf in a subdirectory called invoices within the current directory, use the following relative path:

.invoicesinvoice.pdf

The path instructs the system to locate the invoice.pdf file within the invoices directory in the current working directory. The backslash () separates directories in Windows paths.

Absolute and Relative Paths in Windows

Absolute and relative paths in Windows use backslashes () to separate directories. The start of the absolute path in Windows is the drive letter, a colon, and a backslash (for example, C:).

How to Find an Absolute Path in Windows

There are several ways to find the absolute path of a file or directory in Windows. The most common ways are listed below.

File Explorer

1. Press the Windows key and type File Explorer. Press Enter to open the app.

Opening the file explorer in Windows.Opening the file explorer in Windows.

2. Navigate to the location of the file or directory you want to find the absolute path for.

3. Click the address bar once you’re in the right location. The action displays the full path to the current location, which is the absolute path:

Getting the absolute path in Windows explorer.Getting the absolute path in Windows explorer.

4. Alternatively, right-click the file you want to find the absolute path for and select Properties.

Obtaining a file's properties to see its absolute path in Windows.Obtaining a file's properties to see its absolute path in Windows.

The Properties window contains the file location, i.e., the absolute path:

Checking a file's absolute path in Windows.Checking a file's absolute path in Windows.

Command Prompt

1. Press the Windows key and type Command Prompt. Press Enter to open the the app.

Opening the command prompt.Opening the command prompt.

2. Use the cd command to navigate to the directory containing the file or directory for which you want to see the absolute path.

3. Once you are in the correct directory, run the cd command without any arguments to display the current directory’s absolute path:

cd
Getting the absolute path using the Command Prompt.Getting the absolute path using the Command Prompt.

PowerShell

1. Press the Windows key and type PowerShell. Press Enter to open the app.

Opening Windows PowerShell.Opening Windows PowerShell.

2. Use the cd (Change-Location) command to navigate to the directory containing the file or directory.

3. Once in the correct directory, use the Get-Location cmdlet to display the current directory’s absolute path.

Getting the absolute path using Windows PowerShell.Getting the absolute path using Windows PowerShell.

Absolute and Relative Paths in Linux

Absolute and relative paths in Linux use slashes (/) to separate directories, and the start of the absolute path in Linux is the root directory (/).

Refer to the section below for instructions on how to find the absolute path in Linux.

How to Find an Absolute Path in Linux

There are several methods for finding the absolute path of a file or directory in Linux. The common approaches are listed in the sections below.

The pwd Command

1. Open the terminal by pressing Ctrl+Alt+T.

2. Use the cd command to navigate to the directory containing the file or directory you want to find the absolute path for.

3. Run the pwd (print working directory) command. It displays the absolute path of the current directory.

pwd
Printing the working directory in Linux.Printing the working directory in Linux.

The realpath Command

1. Open the terminal.

2. Use the following syntax to resolve the absolute path of a file or directory, even if it’s a symbolic link or if it contains relative path components:

realpath [/path/to/file]

For example:

Resolving a relative path in Linux.Resolving a relative path in Linux.

The GUI Approach

Use the file explorer in Linux to see the absolute path of a file.

1. In the Files app, navigate to the file for which you want to find the absolute path.

2. Right-click the desired file and select Properties.

Viewing file properties in Linux GUI.Viewing file properties in Linux GUI.

3. In the Properties window, find the Parent folder section, which states the absolute path to the file’s parent directory.

Obtaining a file's absolute path using GUI in Linux.Obtaining a file's absolute path using GUI in Linux.

How to Create a Relative Path?

File paths can also be used to link to external files, such as web pages, images, style sheets, etc. Such paths describe a file’s location in a website’s folder structure.

An absolute path to a file (full URL) uses HTML tags like <a> and <img>, and it is straightforward since it points to the internet URL of the file. On the other hand, relative paths point to a file relative to the current page. Relative file paths start with ./, followed by the path to the local file.

Follow the steps below to link to a file on the web using a relative path:

Step 1: Ensure that the file you want to link to exists on the same server as the HTML file you’re working with. Whether you’re developing a website online or locally, the file you want to link to must be accessible.

Step 2: To create a relative path, the path to the file should be relative to the directory of the HTML file you’re working with. Let’s say you want to link to a file named example.jpg on your current page, and your local HTML file is in the web-development directory.

Step 3: If the example.jpg file is located in a subdirectory named images relative to your HTML file, use ../ to move up one directory level and then specify the images directory. Therefore, the relative path looks like this:

<a href="../images/example.jpg"><img src="../images/example.jpg"></a>

In this example, ../ indicates going up one directory level and then accessing the images directory to reach the desired file.

Adding the relative path in HTML allows you to access the specified file directly without specifying the absolute path that covers all the directories leading to the file. The benefit of using relative paths in HTML is that you can change the domain names easily, and it facilitates moving directories because it doesn’t affect the relative path.

Conclusion

This article has explained the differences between an absolute and relative path and showed how to find a file or directory’s absolute path in Linux or Windows using the GUI or CLI. Additionally, it showed how to create a relative path linking to the URL of the file on the web.

File paths, absolute and relative, are essential for file management, navigation, and referencing in scripting. They are used in various scenarios to interact with and organize files and directories effectively.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !