Linux Command Get File Path: 5 Ways
In this article
The quickest way to get a file's full path in Linux is the realpath command: realpath filename.txt prints the absolute path. You can also use readlink -f, pwd (for the current directory), or the find command with $PWD for more complex searches.
Why You Need the Full Path
In Linux, every file and directory lives somewhere in a tree starting at /. A relative path like docs/report.pdf only makes sense from your current location. An absolute path, such as /home/alice/docs/report.pdf, works from anywhere. You need the full path when writing scripts, setting up cron jobs, configuring services, or sharing a file location with another user. Without it, commands may fail with "No such file or directory" simply because they ran from a different folder.
Method 1: realpath (Simplest)
The realpath command resolves symlinks and prints the canonical absolute path. It's part of GNU coreutils and available on most Linux distributions by default.
- Open a terminal.
- Navigate to the directory containing the file (or use the full relative path).
- Type
realpath filenameand press Enter.
Example:
$ realpath report.pdf /home/alice/docs/report.pdf
If the file doesn't exist, realpath prints an error. Use realpath -m to get a path even if the file doesn't exist (useful for paths you plan to create).
Method 2: readlink -f (For Symlinks)
readlink is designed to show where a symbolic link points. The -f option (canonicalize) follows every symlink in the path and prints the final target. This is especially useful when you have a shortcut like /usr/bin/python3 that actually points to /usr/bin/python3.11.
$ readlink -f /usr/bin/python3 /usr/bin/python3.11
If the file is not a symlink, readlink -f still prints the absolute path, just without resolving anything extra. It behaves similarly to realpath but is more widely available on older systems.
Method 3: pwd (For the Current Directory)
If you're already in the same directory as the file, the full path is simply $(pwd)/filename. The pwd command prints the current working directory. You can combine it with echo:
$ pwd /home/alice/docs $ echo "$(pwd)/report.pdf" /home/alice/docs/report.pdf
This is handy in scripts when you want to construct a full path dynamically. Note that pwd does not resolve symlinks by default; use pwd -P to get the physical directory (without symlinks).
Method 4: find with $PWD (For Searches)
When you don't know exactly where a file is, use find to search and print the full path. The $PWD variable holds the current directory, so you can prepend it to the result.
$ find "$PWD" -name "report.pdf" /home/alice/docs/report.pdf
Without $PWD, find prints paths relative to the starting point (e.g., ./report.pdf). Using $PWD forces absolute paths. You can also use find with -exec realpath for more complex cases, but the above is straightforward.
Method 5: Using ls and Tab Completion
If you're in a graphical file manager or just want a quick visual, you can use ls with the full path. But the most practical trick is to type the filename and press Tab in the terminal. The shell will auto-complete the path, showing the full absolute path if you start with /. For example, type cat /hom and press Tab—it expands to /home/. This isn't a command per se, but it's the fastest way to get a path interactively.
Common Problems and Misconceptions
- Relative vs. absolute: A path without a leading
/is relative. Always userealpathorreadlink -fto get the absolute version. - Symlinks:
pwdshows the logical path (with symlinks) unless you use-P.realpathalways resolves symlinks. - Spaces in filenames: Always quote the filename:
realpath "my file.txt". Unquoted spaces break the command. - File doesn't exist:
realpathandreadlink -freturn an error. Userealpath -mto get a path anyway. - Environment variables:
~expands to your home directory, butrealpathwill expand it correctly.
Frequently Asked Questions
How do I get the full path of a file in a script?
Use realpath "$1" where $1 is the file argument. This works even if the script is called from a different directory. For example, fullpath=$(realpath "$1").
What's the difference between realpath and readlink -f?
Both resolve symlinks and print the canonical path. realpath is a dedicated command and may handle edge cases slightly differently (e.g., nonexistent trailing components). readlink -f is part of the readlink utility and is more portable to older systems. For most uses, they're interchangeable.
Why does pwd not show the full path of a file?
pwd shows the current directory, not a file. To get a file's full path, combine pwd with the filename: echo "$(pwd)/file". If you're in the file's directory, that gives the absolute path.
Can I get the path of a file without typing its name?
Yes, use find with a pattern: find "$PWD" -name "*.pdf" prints all PDF files with full paths. Or use tab completion after typing part of the name.
How do I get the path of the script itself?
Inside a script, use realpath "$0" to get the full path of the script. $0 is the script name as invoked, which may be relative.
Does realpath work on directories too?
Yes, it works on any path, including directories. realpath /home/alice/../bob returns /home/bob after resolving the ...