Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hard links and soft links

Hard links and soft links

Learn the difference between hard links and soft links. This is an internal presentation that I gave at Continuum.

Aaron Meurer

January 23, 2015

More Decks by Aaron Meurer

Other Decks in Programming


  1. First understand a little bit about how filesystems work •

    Every file system is a little different, but the basic ideas are similar. I’ll be describing a very rough version of a (logical) Unix filesystem. • Filesystems are broken up into blocks • Blocks are the smallest unit of data that the filesystem deals with
  2. • There are two basic types of blocks: • data

    blocks • inode blocks (metadata) • inode blocks represent files (or directories). • Every inode has a unique inode number • A file inode holds all the metadata for that file, as well as pointers on disk to all the data blocks for that file (one possibility, could also be a linked list)
  3. • Directory inodes list all the inode numbers for the

    files in that directory • Block locations of all inodes are usually organized in an inode table so that they can be found by number
  4. File inode example (cont.) • The inode contains a lot

    of metadata about the file (mtime, permissions, size) • But note that it does NOT contain the file name or directory location of the file
  5. Directory inode example directory inode 41335 type: directory name: mydir

    pardir: 24312 mtime … inode table file name inode stuff.txt 23351 music.mp3 32154 presentation .pdf 41335 presentation 2.pdf 41335 … …
  6. Directory inode example (cont.) • Notice that I have two

    files with the same inode (presentation.pdf and presentation2.pdf both have inode 41335) • Go back to the original file inode example
  7. Directory inode example (cont.) • The file has a link

    count of 4, meaning that there are four directory listings somewhere in the filesystem referencing it. • Whenever a file is deleted, it’s inode count is decremented. When the count reaches 0, the data is freed.
  8. Hard links • A “hard link” is just a reference

    to an inode • Every file is a hard link! • When two files are “hard links to each other,” they are the exact same inode (i.e., data) on disk.
  9. Hard links (cont.) • If we read or write to

    the files, they will both be the same • Note that some text editors intentionally break hard links
  10. Symlinks • A symbolic link (symlink) is a special kind

    of inode that references a path • Alternately a special entry in a directory listing • Paths can usually be relative or absolute • Symlinks are separate files from the files they point to • Most system calls read through symlinks by default (act as if they were called on the file the symlink points to)
  11. A Python analogy (cont.) • Link counts are like reference

    counts • del a doesn’t delete a, it lowers its reference count. a is deleted when its reference count reaches 0. • Inode numbers are like id(a).