Files and folders
The realpath command
Prints the absolute, real path of a file, with links and .. resolved.
Most useful options
-e, --canonicalize-existingRequires every part of the path to exist, the last one included-m, --canonicalize-missingRequires nothing to exist: it also works for paths that have not been created yet-L, --logicalResolves .. components first, then the symbolic links-s, --strip, --no-symlinksDoes not follow symbolic links: it only gives the absolute path-q, --quietHides most error messages-z, --zeroEnds each output line with a NUL byte instead of a newline--relative-to=DIRPrints the path relative to that folder instead of the absolute oneCopy-ready examples
$ realpath /home/alumno/Documentos/../Descargasresolves the .. and shows the real absolute path.Example output
/home/alumno/Descargas
$ realpath enlacefollows the symbolic link to the path it points to.Example output
/home/alumno/proyectos/web
$ realpath -e informe.txtchecks that the file exists and gives its full path.Example output
/home/alumno/informe.txt
$ realpath -m nueva/carpeta/archivo.txtgives the path of something that does not exist yet, useful for preparing a path before creating it.Example output
/home/alumno/nueva/carpeta/archivo.txt
$ realpath --relative-to=Proyectos /home/alumno/Documentos/informe.txtpath relative to the Proyectos folder, useful for generating relative paths in a script.Example output
../Documentos/informe.txt
You can try them in the browser terminal: it does not touch your computer.
Common errors
| Message | Cause | Fix |
|---|---|---|
| realpath: noexiste/archivo.txt: No such file or directory | An intermediate folder is missing. By default, every part except the last one must exist. | Create the folder with mkdir -p, or use -m if you only want to work out the path. |
| realpath: nuevo.txt: No such file or directory | With -e, the file itself must exist too. | Check the name with ls, or drop -e if you only need the path. |
| realpath: a.txt/x: Not a directory | A middle segment is a file, not a folder. | Check the path: every segment before a slash must be a folder. |
Typical mistakes
- By default the last part may not exist: realpath nuevo.txt works even if the file is not there. With -e it must exist.
- It follows links: if you pass a link, you get the target, not the link. With -s they are not followed.
- A missing intermediate folder makes the command fail; only -m tolerates it.
Differences from similar commands
FAQ
Can it check whether a file exists?
Yes, with -e: it fails if the file is missing, and its exit status works in an if. If that check is all you need, test -e is more direct.
Can I use it to compare two paths?
Yes: if two paths give the same output, they point to the same place, even if one is a link. It is more reliable than comparing the path text.
What does --relative-to do?
It changes the output: instead of the absolute path, it gives the path relative to the folder you name. It is useful for generating relative paths in a script.
Learn to actually use it
The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.
Start the free course