• Links are pointers to files in a different location
  • Compare to shortcuts on other operating systems
  • Links can be useful to make the same file available on multiple locations
  • Linux uses hard links and symbolic links(don’t call it soft links)
  • Create hard links with In and symbolic links with In -s

  • symbolic links points to hard links.
  • hardlinks points to inode
  • inode points to blocks
  • if orginal hard link got removed symbolic links becomes invalid

if you want to see the inode

ls -il

  • look at the file size and permissions of the symbolic link
  • it has size of the string count of the file name
  • it has all the permissions but the actual permissions are on the hardlink

  • Definition: A hard link is an additional name for a file that points directly to the same inode on the disk.
  • Key Features:
    • Shares the same inode number as the original file.
    • If the original file is deleted, the hard link still exists and retains access to the data.
    • Works only on the same file system.
    • Cannot link to directories (to prevent circular references).
  • Use Cases:
    • To create backup references of files without duplicating data.

Example:

ln original_file hard_link

  • Definition: A symbolic link is a special type of file that contains a path to another file or directory.
  • Key Features:
    • Does not share the same inode as the target file.
    • Can span across different file systems.
    • Can link to directories.
    • If the target file is deleted, the symbolic link becomes a “dangling link” and does not work.
  • Use Cases:
    • To create shortcuts or references to files or directories, especially when working across file systems.

Example:

ln -s target_file symbolic_link

FeatureHard LinkSymbolic Link
Inode SharingShares the same inode as the file.Has a separate inode.
File SystemsWorks only within the same file system.Can link across file systems.
DirectoriesCannot link to directories.Can link to directories.
After DeletionFile content remains accessible.Link breaks and becomes unusable.
PerformanceFaster (direct access to inode).Slightly slower (requires path resolution).

Common Commands for Practice

  1. Create Hard Link:

    ln file hardlink
  2. Create Symbolic Link:

    ln -s file symlink
  3. List Links and Check Inodes:

    ls -li
  4. Remove Links:

    • Hard links: rm hardlink
    • Symbolic links: rm symlink