TL;DR
To get the absolute file path in Linux, you can try these six methods:
pwd
command prints the absolute file path of the current working directory.realpath ~/Documents/example.txt
command resolves symbolic links and returns the absolute file path of a file or directory.- The
readlink -f filename
command resolves symbolic links and outputs the absolute path of the specified file in Linux.
Read the article below to find out more about how to get the absolute file path in Linux, its important uses, and some quick troubleshooting tips.
Finding files on your computer can feel like searching for a needle in a haystack, right? Fortunately, thereโs a way to make it easier. In this post, youโll learn about absolute file paths in Linux, which give you the complete address of a file. Iโll show you different methods to find these paths and troubleshoot common issues, making your file management much simpler. By the end, youโll know how to distinguish between absolute and relative paths, and use commands like pwd
, realpath
, Python scripts, and bash scripts to efficiently locate your files.
What is an Absolute File Path?
An absolute file path is the complete address to a file or directory on your computer. It starts from the root directory /
and includes all the directories up to the file or folder. Think of it as the full address of your home, which includes the country, city, street, and house number.
Absolute vs. Relative Paths
Here’s a comparison of Absolute vs. Relative Paths in a table format:
Aspect | Absolute Path | Relative Path |
---|---|---|
Definition | Full path starting from the root directory / | Path relative to the current directory |
Example | /home/user/documents/file.txt | documents/file.txt |
Starting Point | Always starts from the root directory / | Starts from the current directory |
Usage | Provides the full address of a file or directory | Used to navigate within the current directory |
Command Example | nano /home/user/documents/file.txt | nano documents/file.txt |
Scripting Example | cp /home/user/documents/file.txt /home/user/backup/ | cp documents/file.txt backup/ |
Importance of File Paths in Linux Systems
File paths are crucial in Linux because they help the system and users locate files and directories. Knowing the right path ensures that you access and modify the correct files.
1. System Navigation:
To move around in the Linux file system, you use file paths. For example:
- To change directories, you use the
cd
command followed by a path:
cd /home/user/documents
This moves you to the documents
directory.
2. Command Execution:
File paths are essential when running commands that involve files. For instance:
- To open a file with a text editor:
nano /home/user/documents/file.txt
This command opens file.txt
in the nano
text editor.
3. Scripting and Automation:
When writing scripts to automate tasks, you often need to specify file paths. For example:
- A backup script might copy files from one directory to another:
cp /home/user/documents/file.txt /home/user/backup/
This command copies file.txt
to the backup
directory.
How to Get the Absolute File Path in Linux
To get the absolute file path in Linux, use commands like realpath
or readlink -f
. For example, realpath filename
resolves symbolic links and provides the complete path starting from the root directory.
Similarly, readlink -f filename
outputs the absolute path of a file, ensuring accurate path resolution even when dealing with symbolic links. These commands are essential for precise file management and scripting in Linux systems.
For detailed steps for these two methods and four other methods to find absolute file path in Linux, read this guide:
1. pwd Command
The pwd
(print working directory) command prints the absolute file path of the current working directory. Hereโs how to use this command:
- Launch the Terminal by pressing Ctrl+Alt+T or searching for it in your applications.

- Next, navigate to the directory whose absolute path you want to know. You can do this using the
cd
command followed by the directory name or path. For example, you can run the following command to navigate to the Download directory in your home folder.
cd Downloads
- But if you want to go further into the directory, use the slash (
/
) symbol in with thecd
command followed by the directory names. Your command should look like this:

- Once you’re in the directory whose absolute path you want to know, run the
pwd
command. You can do this by simply typing pwd and pressing Enter:
pwd
- After you run the
pwd
command, you should see the absolute path of the current working directory printed on your Terminal screen.

2. realpath Command
The realpath
command resolves all symbolic links in a file path and returns the absolute file path. To get the absolute file path of a file or directory using this command, follow the steps below:
- Launch the Terminal and run the following command:
cd ~/path/to/file_or_directory
Replace /path/to/file_or_directory
with the actual file or directory path.
- Before using the
realpath
command, you need to identify the file or directory whose absolute path you want to know. Make a note of the file or directory path.

- Once you know the file or directory path, run the
realpath
command followed by the path. For example, if you want to get the absolute file path of a file named “example.txt” located in the Documents directory in your home folder, you can run the following command:
realpath ~/Documents/example.txt
Replace /path/to/file_or_directory with the actual file or directory path.
- In my case, I used the
realpath
command to get the absolute file path of the Document1.odt in the Docs directory.

- After running the
realpath
command, you should see the absolute file path of the file or directory printed on the screen. You should get a similar output on your Terminal screen:

3. Python Script
Python script can also be used to get the absolute path of the file. This can be particularly helpful when you need to work with files in different directories or want to avoid relative path issues. Hereโs how you can use this method to get the absolute file path in Linux:
- Head to the Terminal and launch the Python mode by running the following command:
python3
- Then, execute this Python script, which uses the
os.path.abspath()
method to get the absolute file path of a file or directory:
import os file_path = "/path/to/file_or_directory" absolute_file_path = os.path.abspath(file_path) print(absolute_file_path)
Replace /path/to/file_or_directory
with the actual file or directory path.
- Once you run the command, youโll get the absolute path. Your output should look like this:

4. Bash Script
To get the absolute file path in Linux, you can also create a bash script with the readlink
command and shebang
notation. Here are the steps to use this method:
- Create a shebang file using the
touch
command in the Terminal window.

- Right-click and open the script file in the text editor.

- Write the bash script that uses the
readlink
command to get the absolute file path of a file or directory:
#!/bin/bash
path='readlink -f "$(BASH_SOURCE:-$0)"'
echo 'the absolute path is' $path
- Here’s what your code should look like:

- After executing the command
./Filename.sh
, it will show the absolute path of the file or directory.

5. readlink Command with -f Option
The readlink -f command is used to print the absolute path of a file, resolving all symbolic links.
- Access your command window.
- Install readlink if not already installed:
sudo apt-get install coreutils

- Use the readlink command with the -f option:
readlink -f filename
Replace the filename with the name of your desired file.
This command outputs the absolute path of filename.

6. find Command
The find command is a versatile tool for searching files in a directory hierarchy and printing their absolute paths.
- Launch your Terminal window and navigate to your target directory (if needed):
cd /path/to/search/directory

- Use the find command to search for the file:
find $(pwd) -name filename
Replace the filename with the name of your desired file.
The output will be:

5 Common Problems When Getting Absolute File Path
When getting an absolute file path in Linux, you might face some issues. Iโve listed five of them along with their solutions here:
1. ๐ Symbolic Links
Symbolic links can mislead path resolution, leading to incorrect file paths and script failures. Scripts may not follow links correctly, resulting in unintended file operations or errors.
Solution: Use commands like realpath
or readlink -f
to resolve symbolic links and obtain the actual file path without symbolic references.
2. ๐ Permissions
Restricted file permissions can block access to files, causing permission denied errors during path retrieval. This issue often arises when attempting to access system files or directories without sufficient privileges.
Solution: Ensure that the current user has appropriate read permissions for the file or directory. Use sudo
or adjust file permissions (chmod
) if necessary.
3. ๐ Whitespace Handling
Mishandling spaces in file names can disrupt path resolution, causing commands to misinterpret paths. Scripts or commands may fail to recognize files with spaces, leading to errors in file operations.
Solution: Enclose file paths containing spaces within quotation marks ("
). This ensures that the shell interprets the entire path correctly, including spaces.
4. ๐ Relative Path Conversion
Errors in converting relative paths to absolute paths can result in incorrect path outputs, affecting script functionality. Scripts relying on accurate path conversion may fail to locate files or directories correctly.
Solution: Always ensure that the base directory is correctly set before converting relative paths. Use cd
or pwd
commands to establish the starting point for relative path conversions.
5. ๐ Shell Environment
Differences in shell configurations may alter path interpretations, impacting the consistency of path resolutions in scripts. Variables, aliases, or shell settings can influence how paths are resolved, leading to inconsistencies across different shell environments.
Solution: Use consistent shell environments across systems or explicitly define path-related variables ($PATH
, $HOME
) within scripts to ensure uniform path resolution.
Wrapping Up
In conclusion, finding the absolute file path in Linux is essential for navigating, running commands, and scripting. You can use commands like pwd
, realpath
, Python scripts, and bash scripts with readlink
to get these paths. Watch out for issues like symbolic links, permissions, and whitespace errors to troubleshoot effectively.
For further learning, you might find it helpful to explore articles on:
- Displaying hidden files in Linux, which will show you how to manage and view all files in your directories.
- Unzipping .gz files, providing you with the skills to handle compressed files efficiently.
- Sorting files with the
ls
command, helping you organize and access your files more effectively.
Frequently Asked Questions
What is the difference between absolute and relative file paths?
How doย I get the absolute file path of a file or directory using a GUI file manager?
Can I obtain the absolute path for multiple files at once?
realpath
command with the file names separated by spaces. For example, to obtain the absolute file path for files named file1.txt and file2.txt, you can enter the following command realpath file1.txt file2.txt
. This will output the absolute file path for both files. Similarly, you can use this command to get the absolute file path for several files at once.How do I get the absolute file path for a directory instead of a file?
realpath
command with the directory path as the argument. For example, to obtain the absolute file path for a directory named my_directory, you can enter the command realpath /path/to/my_directory. This will diplay the absolute file path for the my_directory directory.