Signup/Sign In

How to Print the Resolved Path (realpath command).

In Linux, if we want to print the resolved path, we use the realpath command.

The realpath command is used to 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. The realpath command and readlink commands display the resolved path for symlinks in the output.

  • If we use the realpath command then it will be the same effect as readlink -f with GNU readlink command.
  • realpath command expands all symbolic links and resolves reference to /. , /../ and extra / characters in the null-terminated string named by the path to producing a canonicalized absolute pathname.

The general syntax of the realpath command

realpath [OPTION]... FILE...

Brief description of options available with the 'realpath' command.

Options Description
-e, --canonicalize-existing all components of the path must exist
-m, --canonicalize-missing no path components need to exist or be a directory
-L, --logical resolve ' ..' components before symlinks
-P, --physical resolve symlinks as encountered (default)
-q, --quiet suppress most error messages
--relative-base=DIR print absolute paths unless paths below DI
--relative-to=DIR print the resolved path relative to DIR
-z, --zero end each output line with NUL, not newline
-s, --strip, --no-symlinks don't expand symlinks
-z, --zero end each output link with NULL, not newline
--help display help and exit
--version output version information and exit

Example: Display the absolute path of the current directory with realpath command

In this example, using realpath . display the absolute path of the current directory here . is used for the current directory. suppose we want to display the absolute path of the parent directory then we have to use .. for the parent directory.

Linux Terminal: Display the absolute path of the current directory.

Example: Display the absolute path without expanding symlinks with realpath command

In this example, using s option with the realpath command will expanding symlinks without an absolute path.

Linux Terminal: Display the absolute path without expanding symlinks

Example: Display the version information and exit for realpath command

for checking the version information of the realpath command execute realpath --version after executing this command the version information of realpath is prompt.

Linux Terminal: Display the version information and exit

Example: Display help and exit (realpath command)

In this tutorial, only an important option with a description is defined for checking more about the realpath command execute realpath --help this will prompt all options available in realpath with a description.

Linux Terminal: Display help and exi

Other useful usage option and syntax realpath command

# Require all path components to exist:
realpath --canonicalize-existing path_to/file_or_directory

# Resolve ".." components before symlinks:
realpath --logical path_to/file_or_directory

# Disable symlink expansion:
realpath --no-symlinks path_to/file_or_directory

# Suppress error messages:
realpath --quiet path_to/file_or_directory

Conclusion

In this tutorial, we covered how to converts each filename argument to an absolute pathname with available options and suitable examples. realpath command has the same effect as readlink -f with GNU readlink.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.