What is the difference between a symbolic link and a hard link?

Solution 1:

Underneath the file system, files are represented by inodes. (Or is it multiple inodes? Not sure.)

A file in the file system is basically a link to an inode.
A hard link, then, just creates another file with a link to the same underlying inode.

When you delete a file, it removes one link to the underlying inode. The inode is only deleted (or deletable/over-writable) when all links to the inode have been deleted.

A symbolic link is a link to another name in the file system.

Once a hard link has been made the link is to the inode. Deleting, renaming, or moving the original file will not affect the hard link as it links to the underlying inode. Any changes to the data on the inode is reflected in all files that refer to that inode.

Note: Hard links are only valid within the same File System. Symbolic links can span file systems as they are simply the name of another file.

Solution 2:

Some examples that might help.

Create two files with data in them:

$ printf Cat > foo
$ printf Dog > bar

Create a hard and soft (aka symbolic) link:

$ ln foo foo-hard
$ ln -s bar bar-soft

List directory contents in long format by increasing size:

ls -lrS
lrwxr-xr-x   1 user  staff        3  3 Apr 15:25 bar-soft -> bar
-rw-r--r--   2 user  staff        4  3 Apr 15:25 foo-hard
-rw-r--r--   2 user  staff        4  3 Apr 15:25 foo
-rw-r--r--   1 user  staff        4  3 Apr 15:25 bar

This tell us that

  • 1st column: the file mode for the soft and hard links differ

    • soft link: lrwxr-xr-x
      • filetype: l = symbolic link
      • owner permissions: rwx = readable, writable, executable
      • group permissions: r-x = readable, not writable, executable
      • other permissions: r-x = readable, not writable, executable
    • hard link: -rw-r--r--
      • filetype: - = regular file
      • owner permissions: rw- = readable, writable, not executable
      • group permissions: r-- = readable, not writable, not executable
      • other permissions: r-- = readable, not writable, not executable
  • 2nd column: number of links is higher for the hard linked files

  • 5th column: the size of the soft link is smaller, because it's a reference as opposed to a copy

  • last column: the symbolic link shows the linked-to file via ->

Changing the filename of foo does not affect foo-hard:

$ mv foo foo-new
$ cat foo-hard
Cat

Changing the contents of foo is reflected in foo-hard:

$ printf Dog >> foo
$ cat foo-hard
CatDog

Hard links like foo-hard point to the inode, the contents, of the file.

This is not the case for soft links like bar-soft:

$ mv bar bar-new
$ ls bar-soft
bar-soft
$ cat bar-soft  
cat: bar-soft: No such file or directory

The contents of the file could not be found because the soft link points to the name, that was changed, and not to the contents.

Likewise, If foo is deleted, foo-hard still holds the contents; if bar is deleted, bar-soft is just a link to a non-existing file.