Files and folders
The readlink command
Shows where a symbolic link points. With -f it prints the resolved absolute path.
Most useful options
-f, --canonicalizeFollows every link along the path. Only the last segment may be missing-e, --canonicalize-existingLike -f, but every segment must exist. If not, it exits printing nothing-m, --canonicalize-missingFollows links without requiring any segment to exist-n, --no-newlineDoes not print the final newline, useful for joining the output with other text-v, --verboseShows error messages. By default, readlink stays silent and only exits with status 1-z, --zeroEnds each output line with a NUL byte instead of a newlineCopy-ready examples
$ readlink -f /home/alumno/Documentos/../Proyectosresolved absolute path of a folder, with no links in between.Example output
/home/alumno/Proyectos
$ readlink enlacethe target stored in the link, exactly as it was written when it was created.Example output
proyectos/web
$ readlink -f enlacefollows the link to the real absolute path.Example output
/home/alumno/proyectos/web
$ readlink cadenaif a link points to another link, it only follows one level.Example output
enlace
$ readlink -v archivo.txta regular file is not a link: with -v the reason shows up.Example output
readlink: archivo.txt: Invalid argument
You can try them in the browser terminal: it does not touch your computer.
Common errors
| Message | Cause | Fix |
|---|---|---|
| readlink: archivo.txt: Invalid argument | The argument exists but is not a symbolic link. Without -v you will not see the message, only the exit status 1. | Check the type with ls -l: a link shows an arrow. If you want the real path of the file, use readlink -f or realpath. |
| readlink: noexiste: No such file or directory | With -v, the name does not exist. Without -v the error does not show, it only exits with status 1. | Check the name with ls. |
| readlink: missing operand | You ran readlink without any path. | Pass the link, for example readlink link. For help, run readlink --help. |
Typical mistakes
- Without -f, -e or -m it follows only one level: if the target is also a link, you see that second link, not the final one.
- The target is shown as it was stored: if it was relative, you see it relative. For the absolute path use -f.
- A regular file is not a link: readlink prints nothing and exits with status 1, with no message, unless you use -v.
Differences from similar commands
FAQ
How do I check whether a file is a link?
With readlink: if it prints something, it is a link and the output is its target. If it prints nothing and exits with status 1, it is not a link or it does not exist. In a script, test -L is more direct.
Why does the target show up with ../?
Because the link was created with a relative path, measured from the folder where the link lives and not from where you are. readlink shows the stored text without reinterpreting it.
What is the -n option for?
It prints the target without a final newline, useful for joining it with other text on the same line. Inside $(...) the final newline disappears anyway, so it is not needed there.
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