Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Linux Command Get File Path: 5 Ways

· 4 min read · Home Tech

In this article
  1. Why You Need the Full Path
  2. Method 1: realpath (Simplest)
  3. Method 2: readlink -f (For Symlinks)
  4. Method 3: pwd (For the Current Directory)
  5. Method 4: find with $PWD (For Searches)
  6. Method 5: Using ls and Tab Completion
  7. Common Problems and Misconceptions
  8. Frequently Asked Questions

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.

  1. Open a terminal.
  2. Navigate to the directory containing the file (or use the full relative path).
  3. Type realpath filename and 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).

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 use realpath or readlink -f to get the absolute version.
  • Symlinks: pwd shows the logical path (with symlinks) unless you use -P. realpath always resolves symlinks.
  • Spaces in filenames: Always quote the filename: realpath "my file.txt". Unquoted spaces break the command.
  • File doesn't exist: realpath and readlink -f return an error. Use realpath -m to get a path anyway.
  • Environment variables: ~ expands to your home directory, but realpath will 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 ...