Přeskočit obsah

TL;DR ✨

The difference between a symbolic link and a hardlink may not be clear from the usual definitions. This article therefore explains it with concrete examples.

In terms of size, ten hardlinks all show the same size. Changing one changes the size reported by all the others, but does not consume that space repeatedly on disk. The same principle applies to symlinks when their target changes, since a symlink only points elsewhere.

From the archive

This article was first published on 2017-11-05. It is also a snapshot of its time, so some details and recommendations may no longer reflect the current situation.

The difference between a symbolic link and a hardlink may not be clear from the usual definitions. This article therefore explains it with concrete examples.

1. General information:

  • Points to a file or directory.
  • Is universal and can be copied between systems.
  • Carries no specific value from the file to which it points.
  • In other words, creating a symlink simply creates a reference to something. It says: click here to get there.
  • Hardlinks can point only to files within their own filesystem.
  • A hardlink points to an inode. When it refers to a file, it also refers to its metadata: permissions, modification times, lists of data blocks, and so on.
  • In other words, creating a hardlink produces a completely equivalent file. It looks like a copy, but is not one: the hardlink and original share the same data and are equivalent.

2. What is the difference between a hardlink and a symlink?

✨ 2.1 A concrete example

Consider this example in the user's home directory:

r@Connectica:~/linky$ ls *
LinkFile: HardOrig.txt  Sym-orig SymOrig.txt
OriginalFile: Hard-orig Sym-orig

The following directories are created:

  1. OriginalFile, containing the originals:
    • Hard-orig, containing HardOrig.txt.
    • Sym-orig, containing SymOrig.txt.
  2. LinkFile, containing symbolic links and hardlinks to directories and files:
    • A hardlink (ln) to /Hard-orig — it does not work (hard link not allowed for directory).
    • A hardlink (ln) to HardOrig.txt.
    • A symlink (ln -s) to /Sym-orig and its content, including SymOrig.txt.
    • A symlink (ln-s) to SymOrig.txt.

2.2 Editing files

2.2.1 Changing text content in the original files

The simplest case comes first. If the content of an original file is edited, the change appears through every link to it.

cat ~/linky/OriginalFile/HardOrig.txt
Hard-A

After editing:

cat ~Linky/LinkFile/HardOrig.txt

Hard-A
Hard-B
cat ~/linky/OriginalFile/Sym-orig/SymText.txt

Sym-A
Po Editaci:
cat ~/linky/LinkFile/SymText.txt
Sym-A
Sym-B
In terms of size, creating ten hardlinks gives all of them the same apparent size. Changing one hardlink changes what all the others refer to, but it does not multiply the space used on disk. A symlink likewise retains its own size when the target file changes because it merely points to something else.

In terms of size, ten hardlinks all show the same size. Changing one changes the size reported by all the others, but does not consume that space repeatedly on disk. The same principle applies to symlinks when their target changes, since a symlink only points elsewhere.

2.2.2 Changing content through linked files:

What happens when the text files are changed in /LinkFile?

cat~/linky/LinkFile/HardOrig.txt
Hard-A
Hard-B

After editing:

cat ~/linky/LinkFile/HardOrig.txt

Hard-A

Hard-B

Hard-C
cat ~/linky/LinkFile/SymText.txt

Sym-A

Sym-B

After editing:

cat ~/linky/LinkFile/SymText.txt
Sym-A
Sym-B
Sym-C

✨ What happened in /OriginalFile?

cat ~/linky/OriginalFile/Hard-orig/HardOrig.txt

Hard-A

Hard-B

Hard-C
cat ~/linky/OriginalFile/Sym-orig/SymOrig.txt

Sym-A

Sym-B

Sym-C
2.2.3 Renaming the original text files

Next, the files targeted by the links are renamed in OriginalFile:

mv  ~/linky/OriginalFile/Hard-orig/HardOrig.txt ~/linky/OriginalFile/HardOrig-prejmenovano.txt
mv  ~/linky/OriginalFile/Sym-orig/SymText.txt ~/linky/OriginalFile/SymText-prejmenovano.txt

✅ The following result appears in ~/linkyLinkFiles/

  • cat Hard-orig.txt works and prints the content.
  • cat SymOrig.txt does not work and reports that the original cannot be found.

Renaming the files back to their original names makes cat SymText.txt work again.

If the linked text files are renamed...

mv ~/linky/LinkFile/HardOrig.txt ~/linky/LinkFile/HardOrig-prejmenovano2.txt
mv ~/linky/LinkFile/SymOrig.txt SymOrig-prejmenovano2.txt

...the original is unaffected:

cd ~/linky/OriginalFile/Hard-orig$ ls

HardOrig.txt

cd ~/linky/OriginalFile/Sym-orig$ ls
 SymText.txt

2.3 Changing directories

2.3.1 Renaming original directories

✨ When the original directory is renamed

mv ~/linky/OriginalFile/Sym-orig/~/linky/OriginalFile/Sym-orig-prejmenovanAdresar/

The result is:

  • cd~/linky/LinkFile/Sym-orig does not work.
  • cat~/linky/LinkFile/SymOrig.txt does not work.

After the names are changed back to the originals, the symlinks work again.

2.3.2 Renaming linked directories

Renaming a linked directory:

mv ~/linky/LinkFile/Sym-orig ~/linky/LinkFile/Sym-orig-prejmenovany

has no effect on the original.

3. Practical examples

✨ 3.1 Categorizing directory structures

Imagine /home/r/Dokumenty with subdirectories such as /smlouvy, /knihy jízd, and /technickeDokumentace, while /home/r/firmy contains company directories such as /Connectica, /ICTMorava, and /Datamasters. The categories overlap: contracts may also belong to ICTMorava, while /fotografie may contain company-related installation or site-survey photographs. Where should these files be placed, and how can the structure remain orderly? Symlinks and hardlinks provide the solution.

On a Synology NAS, symlinks and hardlinks can easily be created after logging in over SSH, but they do not appear in File Station. The solution is simple, although hard to find.

Create and mount a directory:

mkdir /volume1/technickeDokumentace
mount -o bind /volume1/homes/Franta/firmy/Connectica/ /volume1/technickeDokumentace

Then make it permanent by adding this to /etc/rc.local:

mount -o bind /volume1/homes/Franta/firmy/Connectica/ /volume1/technickeDokumentace

The references then become visible when directories are opened in the Synology NAS graphical interface. As a workaround for this limitation, it is the only solution presented here.

🐧 3.2 Synchronizing files between other Linux machines

This issue frustrated the author for a long time. Suppose there is a main Linux machine A and a secondary notebook B, both using Midnight Commander. Manually copying every new shortcut from /.config/mc to each machine is tedious, especially the hotlist file containing all shortcuts. ~/.config/mc therefore needs to be synchronized. Synology CloudStation or a secure alternative such as ownCloud can help; public clouds such as Google Drive or Dropbox should be avoided for this purpose.

  • In CloudStation, enable synchronization of files beginning with ".", meaning hidden files.
  • Enable two-way file synchronization.
  • Configure synchronization so the newest version is always uploaded to the server.

Copy ~/.config/mc/hotlist to ~/Cloudstation/Synchronizace/mc.

Rename the existing hotlist:

mv ~/.config/mc/hotlist ~/.config/mc/hotlist-backup

On machine A, create a hardlink from CloudStation to the configuration:

~/Cloudstation/Synchronizace/mc/hotlist ~/.config/mc/hotlist

Do the same on machine B. Rename its hotlist:

mv ~/.config/mc/hotlist ~/.config/mc/hotlist-backup

Create the hardlink on machine B:

~/Cloudstation/Synchronizace/mc/hotlist ~/.config/mc/hotlist

That is all. Editing the file on either machine updates it on the other one as well, and vice versa.

4. Closing

One final image illustrates everything above. It demonstrates the subject in a clear picture that says more than a thousand words. The author borrowed it from https://i.stack.imgur.com/f7Ijz.jpg.

hardlink, symlink The difference between a hardlink and a symlink