How to Get Absolute Path in Ubuntu Linux
Working with files in Ubuntu often requires knowing the exact location of an item within the directory structure. While relative paths are convenient for navigation, scripts and system configurations frequently demand the full absolute path. This article explains the specific commands used to convert a relative file reference into its complete absolute path within the Ubuntu terminal.
Using the realpath Command
The most straightforward command to display the absolute path of a
relative file reference is realpath. This utility is part
of the GNU coreutils package, which is installed by default on Ubuntu.
It resolves all symlinks and returns the canonicalized absolute
filename.
To use it, open your terminal and type the following command followed by the file or directory name:
realpath filename.txtIf the file is located in a subdirectory, you can pass the relative path directly:
realpath ./documents/report.pdfThe terminal will output the full path starting from the root
directory, such as /home/user/documents/report.pdf.
Using the readlink Command
An alternative method is using the readlink command with
the -f flag. This option forces readlink to
follow symlinks and canonicalize the path, effectively providing the
absolute path similar to realpath.
The syntax is as follows:
readlink -f filename.txtThis command is particularly useful if realpath is
unavailable on a minimal system, though both are standard on modern
Ubuntu installations.
Why Absolute Paths Matter
Relative paths depend on your current working directory, which can change during script execution or user navigation. Absolute paths remain constant regardless of where you are in the filesystem. Using the commands above ensures that your scripts, backups, and system links reference the correct location every time, preventing errors caused by directory mismatches.