Skip to main content

Linux realpath Command

The realpath command converts relative paths into absolute paths.

It also resolves symbolic links.


Why is it Useful?

Suppose:

cd Documents

Current location:

/home/aayan/Documents

File:

notes.txt

Using:

realpath notes.txt

Output:

/home/aayan/Documents/notes.txt

Syntax

realpath [OPTION] FILE

Basic Usage

realpath file.txt

Output:

/home/aayan/file.txt

View Current Directory Absolute Path

realpath .

Output:

/home/aayan

Parent Directory

realpath ..

Output:

/home

Command Options

FlagDescription
-eFile must exist
-mAllow missing components
-sDo not resolve symlinks
--relative-toRelative output
--helpDisplay help

realpath -e

Require file existence.

realpath -e file.txt

realpath -m

Allow missing files.

realpath -m test/file.txt

Output:

/home/aayan/test/file.txt

realpath -s

Do not resolve symbolic links.


Symbolic Link Example

Create:

ln -s Documents docs

Normal:

realpath docs

Output:

/home/aayan/Documents

Using:

realpath -s docs

Output:

/home/aayan/docs

--relative-to

Example:

realpath --relative-to=/home/aayan /home/aayan/Documents

Output:

Documents

Real Examples


PocketMine Server

realpath plugins

Output:

/home/minecraft/server/plugins

Website Files

realpath /var/www/html

PHP Project

realpath app/Controllers

Ethical Hacking

realpath /usr/share/wordlists

Common Mistakes

File does not exist

Use:

realpath -m

Confusing Relative and Absolute Paths

Relative:

Documents/file.txt

Absolute:

/home/aayan/Documents/file.txt

Exercises

realpath .
realpath ..
realpath Documents

Create symlink and test:

realpath
realpath -s

Mini Lab

Create:

mkdir Projects
touch Projects/index.php

Run:

realpath Projects
realpath Projects/index.php

Command Summary

CommandPurpose
realpath fileAbsolute path
realpath .Current directory
realpath ..Parent directory
realpath -eRequire existence
realpath -mAllow missing paths
realpath -sKeep symlinks

Key Takeaways

✅ Converts relative paths into absolute paths.

✅ Resolves symbolic links.

✅ Extremely useful in scripts and automation.

✅ Helpful for server administration and development.


Next Chapter

➡ File Management Commands (mkdir, touch, cp, mv, rm)