Sep 13, 2022 3 min read

Get Absolute File Path in Linux

Table of Contents

You can get the full path of a directory with the pwd command:

pwd

But how do you get the absolute path of a file in Linux?

There are several ways to print the full path of files:

  • readlink
  • realpath
  • find
  • combining ls and pwd
Get file path in Linux
Different ways to print full file path

Let me show you these commands one by one. But before that, I suggest brushing up on the basics of the absolute and relative path concept first.

Absolute vs Relative Path in Linux: What’s the Difference?
In this essential Linux learning chapter, know about the relative and absolute paths in Linux. What’s the difference between them and which one should you use.

The purpose of the readlink command is to resolve symbolic links. You can use it to display the full path of a file like this:

readlink -f filename

Here's an example:

[email protected]:~$ readlink -f sample.txt 
/home/abhishek/sample.txt
[email protected]:

Use realpath to get full file path

The realpath command is used for resolving the absolute file names. Among other uses, it can show the full path to a file.

realpath filename

Take a look at this example:

[email protected]:~$ realpath sample.txt 
/home/abhishek/sample.txt
[email protected]:~$

If you use it with a symbolic link, it will show the real path of the original file. You can force it to not follow the symbolic link:

realpath -s filename

Here's an example where it showed the full path to the source file by default and then I forced it to show the symbolic link, not its original file.

[email protected]:~$ realpath linking-park 
/home/abhishek/Documents/ubuntu-commands.md
[email protected]:~$ realpath -s linking-park 

Use the find command to get the absolute file path

Here's the thing with the find command. Everything is relative to the directory you give it for search location.

If you give it . it will show the relative path. If you give it the absolute path of the directory, you'll get the absolute path of the files you are searching for.

Use the command substitution with the find command like this:

find $(pwd) -name filename

You can run it to find the full path of a single file:

[email protected]:~$ find $(pwd) -name sample.txt
/home/abhishek/sample.txt

Or, you can use it with a bunch of files matching a certain pattern:

[email protected]:~/Documents/eBooks$ find $(pwd) -name "*.pdf"
/home/abhishek/Documents/eBooks/think-like-a-programmer.pdf
/home/abhishek/Documents/eBooks/linux-guide.pdf
/home/abhishek/Documents/eBooks/absolute-open-bsd.pdf
/home/abhishek/Documents/eBooks/theory-of-fun-for-game-design.pdf
/home/abhishek/Documents/eBooks/Ubuntu 1804 english.pdf
/home/abhishek/Documents/eBooks/computer_science_distilled_v1.4.pdf
/home/abhishek/Documents/eBooks/the-art-of-debugging-with-gdb-and-eclipse.pdf

Now this one is a bit tricky and messy.

You can use the environment variable PWD with ls command like this to display the files and directories with their absolute path:

ls -ld $PWD/*

You get an output like this:

[email protected]:~/test$ ls -ld $PWD/*
-r--rw-r-- 1 abhishek abhishek    0 Jul 27 16:57 /home/abhishek/test/file2.txt
drwxrwxr-x 2 abhishek abhishek 4096 Aug 22 16:58 /home/abhishek/test/new

However, to print the full path of a file with the ls command, you'll have to use it like this:

ls -l $PWD/filename

Not the cleanest solution but it works.

[email protected]:~$ ls -l $PWD/sample.txt 
-rw-r--r-- 1 abhishek abhishek 12813 Sep  7 11:50 /home/abhishek/sample.txt
[email protected]:~$ 

Conclusion

I showed four different ways to get the full file path in Linux. The find and ls commands are common while realpath and readlink are hardly known to many Linux users. It's always good to learn new things, isn't it?

Abhishek Prakash
Creator of Linux Handbook and It's FOSS. An ardent Linux user & open source promoter. Huge fan of classic detective mysteries from Agatha Christie and Sherlock Holmes to Columbo & Ellery Queen.
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Linux Handbook.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.