Create hard and soft (symbolic) links

1. Understand and Use Essential Tools

πŸ“˜Red Hat Certified System Administrator (RHCSA – EX200)


1. What are Links in Linux?

In Linux, a link is like a shortcut or a pointer to another file. It allows you to access the same data from multiple places without duplicating the actual file.

There are two types of links:

  1. Hard Link
  2. Soft Link (Symbolic Link)

2. Hard Links

Definition

A hard link is an additional name for an existing file.

  • Both the original file and the hard link point to the same data on disk.
  • Deleting one does not delete the other because the data still exists.
  • Hard links cannot cross different filesystems (i.e., they must be on the same disk/partition).
  • Hard links cannot link to directories (only files).

How it Works

Think of a file as an inode (a data structure storing file data).

  • The original file has an inode.
  • A hard link points to the same inode.
  • If you change the file through the hard link, the changes appear when accessing the original file.

Commands

Create a hard link:

ln original_file.txt hardlink_file.txt

Check the links:

ls -l
  • The link count (second column in ls -l) increases when you create a hard link.

Example:

echo "Hello RHCSA" > file1.txt
ln file1.txt file1_hardlink.txt
ls -li

Output:

12345 -rw-r--r-- 2 user user 13 Feb 19 11:00 file1.txt
12345 -rw-r--r-- 2 user user 13 Feb 19 11:00 file1_hardlink.txt
  • Both files have the same inode number (12345).
  • The 2 indicates two links pointing to the same data.

3. Soft Links (Symbolic Links)

Definition

A soft link (or symbolic link) is like a pointer or shortcut to another file or directory.

  • Soft links have their own inode, different from the original file.
  • They store the path to the original file.
  • If the original file is deleted, the soft link breaks and becomes a β€œdangling link”.
  • Soft links can point to directories.
  • Soft links can cross filesystems, unlike hard links.

Commands

Create a symbolic link:

ln -s original_file.txt softlink_file.txt

Check it:

ls -l

Example:

lrwxrwxrwx 1 user user 13 Feb 19 11:05 softlink_file.txt -> original_file.txt
  • l at the beginning of permissions indicates it is a link.
  • The arrow -> shows the file it points to.

Example Use Cases in IT

  • Linking config files in /etc/ so multiple services can use the same config.
  • Pointing /var/www/html to a different directory for a web server deployment.

4. Key Differences Between Hard and Soft Links

FeatureHard LinkSoft Link (Symbolic Link)
Points toFile’s inodeFile path
Can point to directories?NoYes
Can cross filesystems?NoYes
Breaks if original file deleted?NoYes
Own inode?NoYes
Link typeExact copy pointerShortcut pointer

5. How to Check Links

1. Using ls -l

  • Hard link: same inode number
  • Soft link: l in the first column and shows -> to target

2. Using stat

stat file1.txt
stat file1_hardlink.txt
  • Same inode for hard links
  • Different inode for soft links

6. Common Exam Tasks You Might Be Tested On

  1. Create a hard link to a file and confirm it works.
  2. Create a soft link to a file or directory.
  3. Identify whether a file is a hard link or soft link.
  4. Understand the difference in behavior when the original file is deleted.
  5. Use links in real IT scenarios, like pointing multiple services to the same config file or organizing files efficiently.

7. Quick Tips for the Exam

  • Hard link = ln file1 file2
  • Soft link = ln -s file1 linkname
  • ls -li shows inode numbers for hard links.
  • ls -l shows the target of symbolic links.
  • Deleting a soft link only deletes the link, not the original file; deleting the original file breaks the link.
Buy Me a Coffee