Intro To 'realpath' Command In Linux
2023-05-28 - By Robert Elder
I use the 'realpath' command to obtain a unique representation of a file or directory path:
realpath svb.txt
Using 'realpath' To Canonicalize Paths
If I run the 'realpath' command on this file path:
realpath /home/robert/important/svb.txt
I'll get back the exact same path:
/home/robert/important/svb.txt
However, if I use a slightly different path that refers to the same file:
realpath ../important/svb.txt
I'll get back the original simplified file path:
/home/robert/important/svb.txt
Here's another example that also returns the same path:
realpath ../important/./././svb.txt
/home/robert/important/svb.txt
Using 'realpath' With Symlinks
If I create a symlink to this file and run the 'realpath' command on the symlink:
ln -s svb.txt other-bank.txt
ls -l
lrwxrwxrwx 1 robert robert 7 May 25 10:48 other-bank.txt -> svb.txt
-rw-rw-r-- 1 robert robert 0 May 25 10:46 svb.txt
I'll once again get the original simplified absolute path:
realpath other-bank.txt
/home/robert/important/svb.txt
Using 'realpath' With Non-Existent Files
There are many different flags that change the behavior of the 'realpath' command, such as the '-e':
realpath no-exist.txt
/home/robert/important/no-exist.txt
realpath -e no-exist.txt
realpath: no-exist.txt: No such file or directory
and '-m' flags:
realpath no-exist/other.txt
realpath: no-exist/other.txt: No such file or directory
realpath -m no-exist/other.txt
/home/robert/important/no-exist/other.txt
These flags control what happens if a path component doesn't exist.
Physical Vs. Logical Vs. Symlink
There is also the '-P', and '-L and '-s' flags that control how symlink and relative path components are resolved. If we start with the following collection of directories and files, some of which are symlinks:
ls -l worst
lrwxrwxrwx 1 robert robert 14 May 29 11:01 worst -> let-fail/other
ls -l worst/../failed-bank.txt
lrwxrwxrwx 1 robert robert 10 May 29 11:01 worst/../failed-bank.txt -> lehman.txt
ls -l failed-bank.txt
lrwxrwxrwx 1 robert robert 16 May 29 11:01 failed-bank.txt -> let-fail/svb.txt
You an see how the 'realpath' command will return different values depending on which flags are used:
realpath -P worst/../failed-bank.txt
/home/robert/important/let-fail/lehman.txt
realpath -L worst/../failed-bank.txt
/home/robert/important/let-fail/svb.txt
realpath -s worst/../failed-bank.txt
/home/robert/important/failed-bank.txt
Obtaining Relative Paths With 'realpath'
You can also use the relative to argument to obtain a path that's relative to a directory that you specify:
realpath --relative-to=/home/robert failed-bank.txt
important/let-fail/svb.txt
Setup Commands
Here are the setup commands that were used in the creation of this article:
touch svb.txt
mkdir let-fail
mkdir let-fail/other
touch let-fail/svb.txt
touch let-fail/lehman.txt
ln -s let-fail/svb.txt failed-bank.txt
ln -s let-fail/other worst
ln -s lehman.txt let-fail/failed-bank.txt
And that's why the 'realpath' 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?
|