AZ.dev

Get the full (absolute) path of a file

| macos: 10.x+

Resolve any relative path to its full, absolute path:

realpath filename

Why realpath, Not readlink -f

readlink -f is the common answer on Linux, but -f is a GNU extension. macOS ships BSD readlink, which doesn’t support -f at all. realpath is a separate utility that behaves the same on both, and it ships by default on modern macOS, so it’s the one habit that works everywhere.

When You Don’t Know Exactly Where the File Is

If the file could be anywhere under your current directory, find it first, then resolve it:

find . -name "filename" -exec realpath {} \;

Gotchas

realpath fails if the file doesn’t exist yet. If you need to resolve a path before creating it, most implementations accept -m (--canonicalize-missing on GNU) to resolve it anyway.

For a file you know sits directly in your current directory, echo $PWD/filename is a lighter, dependency-free shortcut, but it won’t resolve .. segments or symlinks the way realpath does.

shell path realpath readlink macos bash zsh

Was this helpful?

Related Entries

Find files modified within a certain time rangeRun a command on every file find matches