Hard Soft Links

inodes

  • The inode (index node) is a data structure in a Unix-style file system that describes a file-system object such as a file or a directory.
  • Each inode stores the attributes and disk block locations of the object's data
  • Since each file has a single inode, the inode number can be thought of the "physical notation" of the file.
  • You can see inode numbers of files by adding -i parameter to ls:
1$ ls -il myfile
229360799 -rw-rw-r-- 1 osboxes osboxes 0 Jan  7 07:30 myfile
3$ 
  • A directory is a list of inodes with their assigned names. The list includes an entry for itself, its parent, and each of its children.
  • A hard link is a directory entry that associates a name with a file.
  • each file must have at least one hard link.
  • Creating additional hard links for a file makes another entry pointing to the same file.
  • A soft link is ANOTHER DIFFERENT FILE. The contet of this file also poins to the original file.
  • Create a new file in the current directory called filea:
    touch filea
  • Create a hard link to this file, with the name fileaa: ln filea fileaa
  • There's a good way to see where your directory entry points to:
    ls -li
    (make sure you notice that filea and fileaa refer to the same i-node, same file)
  • Create a soft link that will point to filea:
    ln -s filea filesa
  • Use ls -il again, to see what entry points where:
  • Note that the new soft-link filesa does not point to filea
  • Instead, it points to another file, that IS the soft link.
    The content of this file is the actual point to filea
  • It is always safe to delete the soft-link, it does not delete the file that it points to.
  • Delete the soft-link:
    rm filesa
  • Use ls -il to look at your files again.
  • Note that there is a 2 in the 2nd field of the command.
    This is the number of the hard links pointing to this file.
  • Delete filea:
    rm filea
  • Use ls -il to see the results:
    Note that fileaa was not deleted, but the number of links was decremented by 1
  • Now remove fileaa:
    rm fileaa
    (the file is removed)