Bash is a Unix shell and command language that is widely used for shell scripting and programming on Linux and other Unix-based operating systems. Understanding the difference between absolute and relative paths in Bash is essential for anyone working with the terminal and shell scripts. In this blog, we’ll explore what absolute and relative paths are, how they’re used in Bash, and the advantages and disadvantages of each type of path.
What are Absolute and Relative Paths?
An absolute path is a file or directory path that specifies the exact location of a file or directory in the file system, starting from the root directory (
/). Absolute paths are always the same, no matter where you are in the file system. For example, the absolute path to the
/home/user/Documents directory would be
On the other hand, a relative path specifies the location of a file or directory relative to the current working directory. The current working directory is the directory in which the terminal or shell script is running. Relative paths are not the same everywhere, but they are relative to the current working directory. For example, if you are in the
/home/user directory and you want to reference the
Documents directory, you would use the relative path
Absolute and Relative Paths Compared
|Advantages of absolute paths
|Disadvantages of absolute paths
|Always the same, no matter where you are in the file system
|Long and difficult to type
|Easy to use in scripts
|May not work on other systems
|Easier to understand and debug
|Advantages of relative paths
|Disadvantages of relative paths
|Short and easy to type
|Less intuitive and harder to understand
|Difficult to debug
How to Use Absolute and Relative Paths in Bash
In Bash, you can use either absolute or relative paths to reference files or directories. To use an absolute path, simply specify the complete path, starting from the root directory (
/). For example, to list the contents of the
/home/user/Documents directory, you would use the following command:
To use a relative path, specify the path relative to the current working directory. For example, to list the contents of the
./Documents directory (relative to the current working directory), you would use the following command:
Examples of Absolute Paths
# Example 1: Creating a backup of a file located at /var/www/itvraag.nl
sudo cp /var/www/itvraag.nl/index.html /var/www/itvraag.nl/index.html.bak
# Example 2: Changing the permissions of a directory located at /home/user/itvraag.nl
sudo chmod 755 /home/user/itvraag.nl
# Example 3: Moving a file from /tmp to /var/www/itvraag.nl
sudo mv /tmp/file.txt /var/www/itvraag.nl
# Example 4: Listing the contents of a directory located at /var/log/itvraag.nl
ls -lh /var/log/itvraag.nl
# Example 5: Creating a symbolic link to a file located at /var/www/itvraag.nl
ln -s /var/www/itvraag.nl/index.html /usr/share/nginx/html/index.html
Examples of Relative Paths
#Example 1: Navigate to a subdirectory within the current working directory
#Example 2: Reference a file in the parent directory
#Example 3: Reference a file in a sibling directory
#Example 4: Reference a file in the current working directory
#Example 5: Reference a file in a directory relative to the home directory
Tips for Working with Absolute & Relative Paths
- Use absolute paths when you want to specify the exact location of a file or directory, regardless of the current working directory.
- Use relative paths when you want to specify the location of a file or directory relative to the current working directory.
- Verify the paths that you use in your scripts to make sure that you have the necessary permissions to access the files and directories that you want to use.
- Practice using both absolute and relative paths in your scripts, so that you become comfortable with both types of paths and know when to use each one.
- Absolute paths specify the exact location of a file or directory in the file system, starting from the root directory (
- Relative paths specify the location of a file or directory relative to the current working directory (
- Absolute paths are easy to use in scripts and easy to understand, but can be long and difficult to type.
- Relative paths are short and easy to type, but can be less intuitive and harder to understand.
- Be mindful of the risks involved when using absolute and relative paths in Bash, and make sure that you have the necessary permissions to access the files and directories that you want to use.
If you’re interested in learning more about Bash, you might want to check out these related topics:
- Command line arguments and options
- Environment variables
- Wildcard characters
- Input/output redirection
Here’s a challenge for you: write a script that takes a directory path as an argument, and then lists the contents of that directory, both using an absolute path and a relative path. The script should print a message to the terminal indicating whether it is using an absolute or relative path. Good luck!