Linux realpath: Find Absolute Pathname For Given Command or File

How do I find out the canonicalised absolute pathname for a given command or file under Linux operating systems?

You need to use the realpath command. It will converts each filename argument to an absolute pathname, which has no components that are symbolic links or the special . (current directory) or .. (parent directory) entries.

ADVERTISEMENTS

The realpath command expands all symbolic links and resolves references to /./, /../ and extra ‘/’ characters in the null-terminated string named by path to produce a canonicalized absolute pathname. The resulting pathname is stored as a null-terminated string, up to a maximum of PATH_MAX butes, in the buffer pointed to by resolved_path. The resulting path will have no symbolic link, /./ or /../ components.

Syntax

The syntax is as follows:

realpath /path/to/file
realpath /path/to/command

Examples

Type the following command:
ls -l /etc/motd
Sample outputs:

lrwxrwxrwx 1 root root 13 2007-10-06 04:52 /etc/motd -> /var/run/motd

/etc/motd is a symbolic link, which points to ./var/run/motd. To verify this and to print real absolute path, enter:
realpath /etc/motd
Sample outputs:

/var/run/motd

The -s option

If option -s is used realpath only removes . and .. directories, but not symbolic links from filename.
realpath -s /path/to/file

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.