Intro To 'readlink' Command In Linux
2024-01-17 - By Robert Elder
I use the 'readlink' command to canonicalize a file or directory path:
ls -l
total 0
-rw-rw-r-- 1 robert robert 0 Jan 12 18:15 config_2023-12-25.txt
lrwxrwxrwx 1 robert robert 21 Jan 12 18:15 master-config.txt -> config_2023-12-25.txt
readlink master-config.txt
config_2023-12-25.txt
Default Behaviour Of 'readlink'
If I run the 'readlink' command on a symbolic link, I'll see the target that the symbolic link points to, and the return code will indicate success:
readlink master-config.txt
config_2023-12-25.txt
echo $?
0
However, if I run the readlink command on a regular file, I won't see any output, and the return code will indicate failure:
readlink config_2023-12-25.txt
(no output)
echo $?
1
Readlink Mode
By default, the 'readlink' command runs in so-called 'Readlink mode':
info readlink
...
‘Readlink mode’
‘readlink’ outputs the value of the given symbolic links. If
‘readlink’ is invoked with an argument other than the name of a
symbolic link, it produces no output and exits with a nonzero exit
code.
...
This mode is intended to only be run on symbolic links, and will return an error code otherwise.
Canonicalization Mode
The readlink command also supports other flags that activate so-called "Canonicalization mode", which prints absolute file paths:
info readlink
‘Canonicalize mode’
‘readlink’ outputs the absolute name of the given files which
contain no ‘.’, ‘..’ components nor any repeated separators (‘/’)
or symbolic links. Note the ‘realpath’ command is the preferred
command to use for canonicalization. *Note realpath invocation::.
In fact, according to the 'readlink' command's own documentation, the 'realpath' command is preferred over the 'readlink' command for canonicalization of file paths:
info readlink
Note the ‘realpath’ command is the preferred command to use for canonicalization.
Flags That Use Canonicalization Mode
"Canonicalization mode" behaves just like the 'realpath' command and is activated by several flags, such as the '-f' flag:
readlink -f no-exist.txt
/home/robert/important/no-exist.txt
echo $?
0
realpath no-exist.txt
/home/robert/important/no-exist.txt
echo $?
0
the '-e' flag
readlink -e no-exist.txt
(no output)
echo $?
1
realpath -e no-exist.txt
realpath: no-exist.txt: No such file or directory
echo $?
1
and '-m' flag:
readlink -m no-exist/other.txt
/home/robert/important/no-exist/other.txt
echo $?
0
realpath -m no-exist/other.txt
/home/robert/important/no-exist/other.txt
echo $?
0
And that's why the 'readlink' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|