• Home
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All
  • About
Linux-Ubuntu | ln -- make links
  1. Notes
  2. Examples
  3. Command Help (man ln)

  1. Notes
    ln source_file target_file
    
    ln source_file ... target_dir

    • Symbolic links work like shortcuts referencing another file.

    • Hard links are literally another name for the same file.
      A hard link file is a link to the original file that contains information about it.
      Both the hard link file and the original file are physically representing the same file (content).

    By default, ln makes hard links.

    Note that when you copy a symbolic link file (or a hard link file), you are copying the original file.
  2. Examples
    • Symbolic Links

      • Create a symbolic link to file1:
        $ ln -s file1 sfile1
        $ ls -al
        -rw-r--r-- 2 mtitek mtitek 8163 10 Jan 21:04 file1
        lrwxrwxrwx 1 mtitek mtitek    5 10 Jan 21:04 sfile1 -> file1

        Note the symbol -> after the symbolic link file name.
        Note also the size of the symbolic link file (5 bytes versus 8163 bytes of the original file).

        You can use the option -i to print the inode number of each file (which show that both are distinct physical files):
        $ ls -al
        4313560874 -rw-r--r-- 2 mtitek mtitek 8163 10 Jan 21:04 file1
        4313560878 lrwxrwxrwx 1 mtitek mtitek    5 10 Jan 21:04 sfile1 -> file1

      • Create multiple symbolic links:
        $ ln -s file1 file2 folder/
        $ ls -al folder/
        lrwxr-xr-x  1 mtitek  mtitek    5 10 Jan 21:04 file1 -> file1
        lrwxr-xr-x  1 mtitek  mtitek    5 10 Jan 21:04 file2 -> file2

    • Hard Links

      • Create a hard link to file1:
        $ ln file1 hfile1
        $ ls -al
        -rw-r--r-- 2 mtitek mtitek 8163 10 Jan 21:04 file1
        -rw-r--r-- 2 mtitek mtitek 8163 10 Jan 21:04 hfile1

        Note the link count (number after after the permission info) show the same number for both files: two links (because we have created a symbolic link to file1).
        Note also the size of the hard link file (both the hard link file and the original file have the same size: 8163 bytes).

        You can use the option -i to print the inode number of each file (which show that both files are referencing the same physical file):
        $ ls -ali
        4313560874 -rw-r--r-- 2 mtitek mtitek 8163 10 Jan 21:04 file1
        4313560874 -rw-r--r-- 2 mtitek mtitek 8163 10 Jan 21:04 hfile1

  3. Command Help (man ln)
    The following options can be used:
    -s
    |Create a symbolic link.
    
    -f
    |If the target file already exists, then unlink it so that the link may occur.
    |The -f option overrides any previous -i options.
    
    -i
    |If the target file already exists, then ask for user confirmation ('y' or 'Y') before unlinking it so that the link may occur.
    |The -i option overrides any previous -f options.
    
    -v
    |Cause ln to be verbose, showing files as they are processed.
© 2025  mtitek