Beginners Guide for Realpath Command on Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

We recently published a detailed article on the use of the readlink command in Linux; if you read that article, you will understand this better.

In short, both are identical tools for finding the original file to which the soft link points. But this tool can also be used to print the absolute path of the referenced files or directories.

So, stick with this article till the end to learn everything about the realpath command in Linux (with practical examples).

Tutorial Details

DescriptionRealpath (Return the Absolute Path of File)
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesrealpath
Internet RequiredNo

Syntax of the Realpath Command

The realpath command takes two arguments: one is the option, and the other is the name of any soft link, file, or directory.

$ realpath [OPTION] /path/to/softlink
$ realpath [OPTION] /path/to/file
$ realpath [OPTION] /path/to/directory

Displaying the Absolute Path of (Soft Link) Files

If you don’t add any options to the realpath command and just give it the name of a file that isn’t a soft link, it will return the absolute path to the file.

$ realpath file.txt

Output:

Print the absolute path to the referenced file

Now, if you pass the soft link as an argument to this command, it will print out the absolute path of the original file, which is further down in the tree.

An example of a deeply nested “file.txt (original file) <- file1.txt (layer 1) -< file2.txt (layer)” in multiple layers.

The following command will directly print the name of the original “file.txt” instead of the next “file1.txt” soft link in the tree.

$ realpath file2.txt

Output:

Print the absolute path of the original file to which the soft link points

Note that if you specify a file that doesn’t exist, this command will take the name from the argument and use your current working directory to show you the absolute path of the referenced file.

$ ls -l file.txt
$ realpath file.txt

Output:

Print the absolute path of a non existing file

In the case of a soft link, if the original file from where all the soft links originated is deleted, then this command will still point to the source file and show you the absolute path.

$ ls -l file.txt
$ realpath file2.txt

Output:

Print the absolute path of the original file to which the soft link points

However, you can avoid printing the absolute path of the non existing file or soft link by following the next section.

Do Not Print File or Soft Link Path With Deleted Source

When you specify the “-e” flag with realpath, and if the assigned file name you provide as an argument to this command does not exist, then it will throw an error.

$ ls -l file.txt
$ realpath -e file.txt

Output:

Do not print the path of a non existing file name

If the given argument is a soft link to a source file that has been deleted, it will also throw an error.

$ ls -l file.txt
$ realpath -e file2.txt

Output:

Do not print the path of an invalid soft link

In the case of a soft link, it’s trying to reach the original file, which does not exist. However, if your interest is in the absolute path of the specified soft link, then you can get the absolute path of the specified soft link by following the next section.

Do not Expand the Soft Link

Until now, whenever you specified the soft link as an argument to this command, it always tried to print the absolute path of the original source file from where all the soft links originated.

But if you use the “-s” flag, you can avoid this and get the absolute path of the soft link given as an argument to this command.

$ realpath -s file2.txt

Output:

Print the absolute path of the specified soft link

If the original file to which the specified soft link is pointing is deleted, then the above command will still give you the absolute path of the specified soft link.

However, that can be avoided by using the combination of “-e” and “-s” flags.

$ ls -l file.txt
$ realpath -e -s file2.txt

The above command will only return the absolute path of the specified soft link if its original file to which its points still exists.

Print the absolute path of specified soft link if the original file exist

Avoid the Trailing Delimiter

The “-z” flag will not create a new line when this command is executed.

$ realpath -z file.txt

Output:

Do not create a new file

Return the Output in Quiet Mode

The “-q” flag will suppress most of the error by printing the output quietly.

For example, the “file.txt” is deleted, so when this flag is used with the “-e” flag, it gives you no output (indirectly suppressing the error).

$ ls -l file.txt
$ realpath -eq file.txt

Output:

Do not print the output with error

That was the last example of this command.

If you have any questions or queries related to this topic, then feel free to ask them in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.