hard links

A hard link is one file which exists in multiple locations.

Each file has an ID, which is kept on the hard disk's partition. Each hard link has the same ID, because they are the same file. This ID is called the 'inode'.

Create a file, and a hard link:

1fortune > $file_1
2mkdir -p x/y/z/
3ln $file_1 x/y/z/$file_2

Have a long look at the file with the -l flag, and check the inode with -i:

1ls -li $file_1 x/y/z/$file_2

Since they are the same file, you can make a change to one, and it changes both:

1fortune | tee x/y/z/$file_2
2cat $file_1
3cat x/y/z/$file_2

Danger Zone

  • hard links will not work on directories, only standard files and fifos.
  • git will destroy and remake files, so it will not respect hard links.
  • Files cannot have a hard link on another disk partition, because the inode is stored on each partition.