Intro To 'link' Command In Linux
2023-07-26 - By Robert Elder
I use the 'link' command to create hard links in my filesystem:
link target_file link_name
Creating A Hard Link Using The 'link' Command
Here, I have a file named 'favourite-videos.txt' that contains a list of my favourite YouTube videos:
https://www.youtube.com/watch?v=wcRB3TWIAXE
https://www.youtube.com/watch?v=yzC4hFK5P3g
https://www.youtube.com/watch?v=AyzOUbkUf3M
https://www.youtube.com/watch?v=rfIAKctk7xY
https://www.youtube.com/watch?v=ZIjBRA2S0NQ
https://www.youtube.com/watch?v=o856gE8xmls
ls -l
-rw-rw-r-- 1 robert robert 264 May 31 21:54 favourite-videos.txt
I can use this command to create a hard link named 'best-videos.txt' that references the 'favourite-videos.txt' file:
link favourite-videos.txt best-videos.txt
ls -l
total 8
-rw-rw-r-- 2 robert robert 264 May 31 21:54 best-videos.txt
-rw-rw-r-- 2 robert robert 264 May 31 21:54 favourite-videos.txt
Now, I can access the contents of the file using either file name:
cat favourite-videos.txt
https://www.youtube.com/watch?v=wcRB3TWIAXE
https://www.youtube.com/watch?v=yzC4hFK5P3g
https://www.youtube.com/watch?v=AyzOUbkUf3M
https://www.youtube.com/watch?v=rfIAKctk7xY
https://www.youtube.com/watch?v=ZIjBRA2S0NQ
https://www.youtube.com/watch?v=o856gE8xmls
cat best-videos.txt
https://www.youtube.com/watch?v=wcRB3TWIAXE
https://www.youtube.com/watch?v=yzC4hFK5P3g
https://www.youtube.com/watch?v=AyzOUbkUf3M
https://www.youtube.com/watch?v=rfIAKctk7xY
https://www.youtube.com/watch?v=ZIjBRA2S0NQ
https://www.youtube.com/watch?v=o856gE8xmls
The 'link' Command Versus The 'ln' Command
All of the functionality of the 'link' command is also included in the 'ln' command, however the documentation claims that the 'link' command is more portable:
info link
...
On a GNU system, this command acts like ‘ln --directory
--no-target-directory FILENAME LINKNAME’. However, the ‘--directory’
and ‘--no-target-directory’ options are not specified by POSIX, and the
‘link’ command is more portable in practice.
...
Unlike the 'ln' command, the 'link' command does not create symbolic links.
Hard Links Versus Symbolic Link Inode Differences
Hard links differ from soft links since hard links will reference the same filesystem inode, but soft links do not. If we add an additional symbolic link called 'great-videos.txt' for comparison:
ln -s favourite-videos.txt great-videos.txt
You can now see in the output below that the symbolic link uses a different inode number, but the hard link does not:
ls -li
1841709 -rw-rw-r-- 2 robert robert 264 May 31 21:54 best-videos.txt
1841709 -rw-rw-r-- 2 robert robert 264 May 31 21:54 favourite-videos.txt
1868847 lrwxrwxrwx 1 robert robert 20 May 31 22:21 great-videos.txt -> favourite-videos.txt
As a consequence of this, the link command cannot create a link between two different filesystems. If you try it, you'll see an error like the following:
link /my-external-disk-1/hello.txt ~/hello-indirect.txt
link: cannot create link '/home/robert/hello-indirect.txt' to '/my-external-disk-1/hello.txt': Invalid cross-device link
Instead, you can use the 'ln -s' to create 'links' between filesystems:
ln -s /my-external-disk-1/hello.txt ~/hello-indirect.txt
Furthermore, hard links of the same inode share permissions:
ls -li
1841709 -rw-rw-r-- 2 robert robert 264 May 31 21:54 best-videos.txt
1841709 -rw-rw-r-- 2 robert robert 264 May 31 21:54 favourite-videos.txt
chmod o+rwx best-videos.txt
ls -li
1841709 -rw-rw-rwx 2 robert robert 264 May 31 21:54 best-videos.txt
1841709 -rw-rw-rwx 2 robert robert 264 May 31 21:54 favourite-videos.txt
while soft links on Linux always have the full permissions granted:
ls -li
1868847 lrwxrwxrwx 1 robert robert 20 May 31 22:21 great-videos.txt -> favourite-videos.txt
And that's why the 'link' 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?
|