Lesson 1.9: Create hard and soft links


Comparison ParametersHard linkSoft link
Inode number* Files that are hard linked take the same inode number. Files that are soft linked take a different inode number.
Directories Hard links are not allowed for directories. (Only a superuser* can do it) Soft links can be used for linking directories.
File system It cannot be used across file systems. It can be used across file systems.
Data Data present in the original file will still be available in the hard links. Soft links only point to the file name, it does not retain data of the file.
Original file’s deletion If the original file is removed, the link will still work as it accesses the data the original was having access to. If the original file is removed, the link will not work as it doesn’t access the original file’s data.
SpeedHard links are comparatively faster.Soft links are comparatively slower.

Creating a Soft Link

  • Use the command ln -s <source file/dir> <link name> to create a soft link.
  • For example, to create a soft link named softdoc1 for the file /home/sanjeeb/Documents/doc1, you can execute:
[sanjeeb@server ~]$ ln -s /home/sanjeeb/Documents/doc1 /home/sanjeeb/softdoc1
  • Soft links are typically displayed in cyan color for easy identification.
  • To view the source of a soft link, employ the ls -l command. The permissions column will begin with 'l', indicating it's a soft link.
[sanjeeb@server ~]$ ls -lh lrwxrwxrwx. 1 sanjeeb sanjeeb 28 Apr 6 20:54 softdoc1 -> /home/sanjeeb/Documents/doc1
  • Soft links function as pointers, reflecting changes made in either the soft link or the original file.
  • If the background color is black and text color is red, then it denotes that the source of the soft link file is not present or deleted.

Creating a Hard Link

  • To establish a hard link between two files, use the ln command followed by the name of the source file and the desired name for the hard link.
  • For example, to create a hard link named testfile1 for the file file1, execute:
[sanjeeb@server ~]$ ln /home/sanjeeb/Documents/doc2 /home/sanjeeb/harddoc2
  • Unlike copying files using the cp command, where the copied file is independent of the original, hard links maintain a direct relationship with the original file. This means changes made to either file are reflected in both.
  • To confirm the creation of the hard link and ensure it shares properties with the original file, use the ls -l command. The size, date and time, and other attributes will be identical between the original and hard link files.
  • Additionally, you can use the -i option with ls to display files along with their inode numbers. Files with the same inode number are hard links to the same underlying data. This can be verified by comparing the inode numbers of the original file and its hard link.
[sanjeeb@server ~]$ ls -lhi 543913 -rw-r--r--. 2 sanjeeb sanjeeb 24 Apr 6 21:20 harddoc2 [sanjeeb@server Documents]$ ls -lhi total 4.0K 543913 -rw-r--r--. 2 sanjeeb sanjeeb 24 Apr 6 21:20 doc2

Soft Link

In soft link, if the source file is deleted then the soft file will also be deleted.

Command: ln -s < source file/dir > < Link name >

[sanjeeb@assignmentserver dir2]$ ln -s /home/sanjeeb/class/dir1/file1 softfile1 [sanjeeb@assignmentserver dir2]$ ls -lh total 0 lrwxrwxrwx. 1 sanjeeb sanjeeb 30 Sep 19 20:03 softfile1 -> /home/sanjeeb/class/dir1/file1 [sanjeeb@assignmentserver dir1]$ cat file1 This is file1. [sanjeeb@assignmentserver dir2]$ cat softfile1 This is file1. # Now editing softlinked file [sanjeeb@assignmentserver dir2]$ vim softfile1 [sanjeeb@assignmentserver dir2]$ cat softfile1 This is file1. Edited from softlinked file. # New line addded [sanjeeb@assignmentserver dir1]$ cat file1 This is file1. Edited from softlinked file. # Removing the main file for soft linked file [sanjeeb@assignmentserver class]$ tree . ├── dir1 │   └── file1 └── dir2 └── softfile1 -> /home/sanjeeb/class/dir1/file1 [sanjeeb@assignmentserver class]$ rm -f /home/sanjeeb/class/dir1/file1 [sanjeeb@assignmentserver class]$ tree . ├── dir1 └── dir2 └── softfile1 -> /home/sanjeeb/class/dir1/file1 2 directories, 1 file [sanjeeb@assignmentserver class]$ cat /home/sanjeeb/class/dir2/softfile1 cat: /home/sanjeeb/class/dir2/softfile1: No such file or directory

Hard Links

  • Files having same inode number are hardlinks
  • If the main file is deleted then also, The linked file remains as it is.
  • inode number is same
  • Size, date time, everything is same
[sanjeeb@assignmentserver dir2]$ ln /home/sanjeeb/class/dir1/file1 hardfile1 [sanjeeb@assignmentserver class]$ tree . ├── dir1 │   └── file1 └── dir2 └── hardfile1 2 directories, 2 files # Everything is same for the hardlink as the main file [sanjeeb@assignmentserver class]$ ls -lh dir2 total 4.0K -rw-r--r--. 2 sanjeeb sanjeeb 17 Sep 19 20:15 hardfile1 [sanjeeb@assignmentserver class]$ ls -lh dir1 total 4.0K -rw-r--r--. 2 sanjeeb sanjeeb 17 Sep 19 20:15 file1 # The inode number is also same for hardlinks [sanjeeb@assignmentserver dir1]$ ls -i 19548937 file1 [sanjeeb@assignmentserver dir2]$ ls -i 19548937 hardfile1 # Removing the main file to check if hard link exists - yes [sanjeeb@assignmentserver dir1]$ rm -rf file1 [sanjeeb@assignmentserver dir2]$ cat hardfile1 This is file1. Editing the file from hardlink.
All systems normal

© 2025 2023 Sanjeeb KC. All rights reserved.