Skip to content

Latest commit

 

History

History
209 lines (132 loc) · 5.7 KB

File metadata and controls

209 lines (132 loc) · 5.7 KB

Symbolic Links & Hard Links

In Linux, the ln command is used to create links between files. Links can be symbolic (soft) links or hard links, each with unique properties and use cases.

Symbolic Links (Soft Links)

Definition

  • A symbolic link is a special type of file that points to another file or directory. It acts as a shortcut and contains a reference to the target file or directory.
  • Symbolic links can span across different filesystems and partitions.

Creation

  • Command:

    ln -s [TARGET] [LINK_NAME]
  • Example:

    ln -s /path/to/original/file.txt /path/to/link/file_link.txt
    • Creates a symbolic link named file_link.txt pointing to file.txt.

Characteristics

  • Symbolic links can point to directories.

  • They break if the target file or directory is moved or deleted.

  • Easily identifiable by an arrow (->) when using ls -l:

    lrwxrwxrwx 1 user user   11 Aug  2 12:34 file_link.txt -> file.txt
    
  • Can be used to link across different filesystems.

Use Cases

  • Creating shortcuts to frequently used files or directories.
  • Linking libraries or configuration files without duplicating content.
  • Managing versioned files and directories by pointing a fixed name link to different versions.

Hard Links

Definition

  • A hard link is a direct reference to the data of a file. Both the original file and the hard link point to the same inode, making them indistinguishable.
  • Hard links share the same data blocks on the disk and only exist within the same filesystem.

Creation

  • Command:

    ln [TARGET] [LINK_NAME]
  • Example:

    ln /path/to/original/file.txt /path/to/link/file_link.txt
    • Creates a hard link named file_link.txt pointing to file.txt.

Characteristics

  • Hard links cannot point to directories.

  • They do not break if the original file is moved or deleted, as they share the same data.

  • Identical file properties (same inode) and appear as normal files in ls -l:

    -rw-r--r-- 2 user user 1234 Aug  2 12:34 file.txt
    -rw-r--r-- 2 user user 1234 Aug  2 12:34 file_link.txt
    
  • Cannot link across different filesystems.

Use Cases

  • Ensuring redundancy and backup without extra disk space usage.
  • Sharing large files between users without duplicating the data.
  • Implementing file version control where the content remains unchanged.

Comparing Symbolic and Hard Links

Here's a quick comparison between symbolic links and hard links:

Feature Symbolic Links Hard Links
Creation Command ln -s [TARGET] [LINK_NAME] ln [TARGET] [LINK_NAME]
Target Type Files or directories Files only
File System Can span across filesystems Same filesystem only
Inode Separate inode from the target Same inode as the target
Link Breaking Breaks if target is deleted/moved Remains valid unless inode is deleted
Usage Shortcuts, configuration files Backup, file sharing

Basic Commands

Create a Symbolic Link

ln -s /usr/local/bin/python3 /usr/bin/python
  • Creates a symbolic link named python pointing to python3.

Create a Hard Link

ln /home/user/file.txt /home/user/file_link.txt
  • Creates a hard link named file_link.txt pointing to file.txt.

List Links and Inodes

ls -li
  • Displays files with inode numbers and link information.

Remove Links

  • Remove Symbolic Link:

    rm /path/to/symbolic_link
  • Remove Hard Link:

    rm /path/to/hard_link
  • Removing a symbolic or hard link does not affect the target file itself.

Checking Links

Verify Symbolic Link

To check if a symbolic link is correctly pointing to its target:

ls -l /path/to/link
  • Output shows the link with an arrow (->) pointing to the target.

Verify Hard Link

To find files sharing the same inode (i.e., hard links):

ls -li /path/to/directory
  • Compare the inode numbers. Files with the same inode are hard links to the same data.

Example Output

123456 -rw-r--r-- 2 user user  1234 Aug  2 12:34 original_file.txt
123456 -rw-r--r-- 2 user user  1234 Aug  2 12:34 hard_link.txt

Practical Use Cases

1. Symbolic Links for Config Files

  • Quickly switch between different configuration files:

    ln -s ~/config/config_v1.cfg ~/app/config.cfg
    • Allows easy updates to config.cfg by pointing to different versions.

2. Hard Links for Data Backup

  • Create a backup without extra space:

    ln /data/important.doc /backup/important.doc
    • Ensures both files point to the same data, conserving disk space.

3. Linking Shared Libraries

  • Symbolically link shared libraries in development environments:

    ln -s /usr/lib/libXYZ.so /usr/local/lib/libXYZ.so
    • Simplifies access and organization of shared resources.

Summary

  • Symbolic Links (Soft Links): Best for shortcuts, spanning filesystems, and linking directories. Use the ln -s command.
  • Hard Links: Ideal for data backup, redundancy, and ensuring multiple access points to the same file data. Use the ln command.

By understanding these differences and usage scenarios, you can effectively manage files and directories in your Linux environment, leveraging the power of symbolic and hard links. This cheatsheet serves as a quick reference for using the ln command efficiently.