Get the Full Path of a File in Linux

Every file and folder in Linux has a path that directs the user to it. This path is required for programs and scripts to locate and access files. There are various ways to locate the path to a file or folder if you need to. We can get a full file path with different commands on a Linux machine.

In this tutorial, we'll show you how to obtain a file's complete path in Linux using multiple command-line methods.

Understanding File Paths in Linux

In Linux, there are two different kinds of paths: absolute and relative. A forward slash (/) designates the root directory, which is where an absolute path always begins. On the other hand, a relative path begins in the current directory.

The root directory is the top-level directory in the file system, represented by the forward slash (/) symbol. For example, the absolute path to the file "script" is /home/webmaster/script. If we are under /home/webmaster directory, the relative path to /home/webmaster/script is ./script.

Linux File Path Structure / (root) home usr tmp webmaster Absolute: /home/webmaster/file.txt Relative: ./file.txt

Using the readlink Command

The readlink command is used to resolve symbolic links and can display the absolute path of any file. The -f flag canonicalizes the path by following symbolic links.

Example

$ readlink -f new.txt

Output

/tmp/new.txt

Using the realpath Command

The realpath command resolves absolute file names by eliminating symbolic links and relative path components like . and ...

Example

$ realpath hsperfdata_root

Output

/tmp/d1/hsperfdata_root

Using the ls Command with PWD

The ls command when used with the environment variable $PWD prints the full path of the file along with detailed file information including permissions, ownership, and timestamps.

Example

$ ls -l $PWD/nates.txt

Output

-rw-r--r-- 1 webmaster webmaster 0 Oct 16 07:31 /tmp/nates.txt

Using the find Command

The find command searches for files in a directory hierarchy and can display absolute paths. It's particularly useful when you need to locate files by name across multiple directories.

Example

$ find $PWD -type f -name nates.txt

Output

/tmp/nates.txt

Comparison of Methods

Command Best Use Case Key Feature
readlink -f Resolving symbolic links Follows symlinks to show actual target
realpath Canonical absolute paths Removes relative components (. and ..)
ls -l $PWD/ File details with path Shows permissions, size, and timestamps
find $PWD -name Searching by filename Locates files across directory tree

Conclusion

Linux provides multiple commands to determine a file's full path, each with specific advantages. The readlink and realpath commands are most direct for path resolution, while ls and find offer additional functionality for file management tasks.

Updated on: 2026-03-17T09:01:38+05:30

44K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements