HOWTO · Bash
How to Get Absolute Path in Bash
This article demonstrates how to get an absolute path in Linux.
On this page
Use realpath, readlink -f, or find to print a file’s absolute path in Bash. An absolute path starts at /, so it identifies a location independently of your current directory. The commands below use the same temporary file to show how each method works.
How to Get an Absolute Path in Bash
An absolute path begins with the root directory /, for example /home/sam/notes/report.txt. A relative path such as notes/report.txt is interpreted from the current working directory. The shell’s $PWD variable normally contains that directory’s absolute path.
Run this example:
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
mkdir -p "$work/notes"
printf 'sample\n' > "$work/notes/report.txt"
cd "$work"
printf 'realpath: '
realpath notes/report.txt
printf 'readlink: '
readlink -f notes/report.txt
printf 'find: '
find "$PWD" -type f -name 'report.txt' -print
This creates one file in a temporary directory, prints its path with each command, and removes the directory when the shell exits. The example assumes a Linux environment that provides realpath and a readlink implementation with -f support; availability and details can vary across systems.
Understand Absolute and Relative Paths
The leading slash is the useful test: /var/log/syslog is absolute, while var/log/syslog is relative. Running cd /tmp changes the base used for relative paths. The same relative name can therefore refer to different files from different working directories.
An absolute path is useful when a command, script, or configuration needs an unambiguous location. A relative path is often shorter when working inside a project. realpath can resolve a relative input against the current directory and print the resulting canonical path; use $PWD as the starting point when searching with find so its printed file paths are absolute.
Use realpath for an Absolute Path
Pass the file name or path to realpath, for example realpath notes/report.txt. The command resolves the path from the current directory and prints a canonical absolute path. Canonicalization can resolve symbolic links and remove redundant path components. Check the utility’s local documentation if you need precise behavior for a path whose final file does not exist, because options and behavior may differ by implementation.
realpath is a direct choice when the utility is installed and the target path is known. It reports one resolved path, rather than searching a directory tree for matching names.
Use readlink for an Absolute Path
Use readlink -f notes/report.txt on Linux systems whose implementation supports -f. The -f option asks readlink to canonicalize the path, following symbolic links in its path components and producing an absolute result. Plain readlink without -f has a different purpose: it prints a symbolic link’s stored target, which may itself be relative.
The -f option is not available in every readlink implementation. If the command reports an unsupported option, check the system’s manual page or use an available realpath implementation. Do not assume that all systems handle missing path components identically.
Find a File and Print Its Absolute Path
Use find when you know a directory to search but not the target’s exact location. Start from an absolute directory such as "$PWD", then filter by type and name: find "$PWD" -type f -name 'report.txt' -print. Quoting $PWD preserves spaces in the directory name, and quoting the pattern keeps the shell from expanding it before find receives it.
A search can print more than one result if several files match. Review the full output when names are not unique, or narrow the search by choosing a more specific starting directory or pattern. Because the starting point is absolute, paths printed by find are absolute as well.