NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |ATTRIBUTES |VERSIONS |STANDARDS |HISTORY |BUGS |SEE ALSO |COLOPHON | |
realpath(3) Library Functions Manualrealpath(3)realpath - return the canonicalized absolute pathname
Standard C library (libc,-lc)
#include <limits.h>#include <stdlib.h>char *realpath(const char *restrictpath,char *restrictresolved_path); Feature Test Macro Requirements for glibc (seefeature_test_macros(7)):realpath(): _XOPEN_SOURCE >= 500 || /* glibc >= 2.19: */ _DEFAULT_SOURCE || /* glibc <= 2.19: */ _BSD_SOURCE
realpath() expands all symbolic links and resolves references to/./,/../ and extra '/' characters in the null-terminated string named bypath to produce a canonicalized absolute pathname. The resulting pathname is stored as a null-terminated string, up to a maximum ofPATH_MAXbytes, in the buffer pointed to byresolved_path. The resulting path will have no symbolic link,/./ or/../ components. Ifresolved_path is specified as NULL, thenrealpath() usesmalloc(3) to allocate a buffer of up toPATH_MAXbytes to hold the resolved pathname, and returns a pointer to this buffer. The caller should deallocate this buffer usingfree(3).
If there is no error,realpath() returns a pointer to theresolved_path. Otherwise, it returns NULL, the contents of the arrayresolved_path are undefined, anderrno is set to indicate the error.
EACCESRead or search permission was denied for a component of the path prefix.EINVALpath is NULL. (Before glibc 2.3, this error is also returned ifresolved_path is NULL.)EIOAn I/O error occurred while reading from the filesystem.ELOOPToo many symbolic links were encountered in translating the pathname.ENAMETOOLONG A component of a pathname exceededNAME_MAXcharacters, or an entire pathname exceededPATH_MAXcharacters.ENOENTThe named file does not exist.ENOMEMOut of memory.ENOTDIR A component of the path prefix is not a directory.
For an explanation of the terms used in this section, seeattributes(7). ┌──────────────────────────────────────┬───────────────┬─────────┐ │Interface│Attribute│Value│ ├──────────────────────────────────────┼───────────────┼─────────┤ │realpath() │ Thread safety │ MT-Safe │ └──────────────────────────────────────┴───────────────┴─────────┘
GNU extensions If the call fails with eitherEACCESorENOENTandresolved_path is not NULL, then the prefix ofpath that is not readable or does not exist is returned inresolved_path.
POSIX.1-2008.
4.4BSD, POSIX.1-2001, Solaris. POSIX.1-2001 says that the behavior ifresolved_path is NULL is implementation-defined. POSIX.1-2008 specifies the behavior described in this page. In 4.4BSD and Solaris, the limit on the pathname length isMAXPATHLEN(found in<sys/param.h>). SUSv2 prescribesPATH_MAX andNAME_MAX, as found in<limits.h> or provided by thepathconf(3) function. A typical source fragment would be #ifdef PATH_MAX path_max = PATH_MAX; #else path_max = pathconf(path, _PC_PATH_MAX); if (path_max <= 0) path_max = 4096; #endif (But see the BUGS section.)
The POSIX.1-2001 standard version of this function is broken by design, since it is impossible to determine a suitable size for the output buffer,resolved_path. According to POSIX.1-2001 a buffer of sizePATH_MAXsuffices, butPATH_MAXneed not be a defined constant, and may have to be obtained usingpathconf(3). And askingpathconf(3) does not really help, since, on the one hand POSIX warns that the result ofpathconf(3) may be huge and unsuitable for mallocing memory, and on the other handpathconf(3) may return -1 to signify thatPATH_MAXis not bounded. Theresolved_path == NULL feature, not standardized in POSIX.1-2001, but standardized in POSIX.1-2008, allows this design problem to be avoided.
realpath(1),readlink(2),canonicalize_file_name(3),getcwd(3),pathconf(3),sysconf(3)
This page is part of theman-pages (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report for this manual page, see ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩. This page was obtained from the tarball man-pages-6.15.tar.gz fetched from ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on 2025-08-11. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgLinux man-pages 6.15 2025-05-17realpath(3)Pages that refer to this page:dpkg-shlibdeps(1), readlink(1), realpath(1), mount(2), readlink(2), bindtextdomain(3), canonicalize_file_name(3), matchpathcon(3), selinux_restorecon(3), mount(8), restorecon(8)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |