
what is soft&hard links
Hard Link acts like a mirror copy of the original file. These links share the same inodes. Changes made to the original or hard linked file will reflect in the other. When you delete Hard Link nothing will happen to the other file. Hard links can’t cross file systems.
Soft Link is actual link to the original file. These Links will have a different Inodes value. Soft link points to the original file so if original file is deleted then the soft link fails. If you delete the Soft Link, nothing will happen to file. The reason for this is, the actual file or directory’s inode is different from the “soft link” created file’s inodes. Hard links can cross file systems.
Hard and soft link for Files
mohammedrafi@NOC-RAFI:~$ mkdir inode
mohammedrafi@NOC-RAFI:~$ cd inode/
mohammedrafi@NOC-RAFI:~/inode$ cat >first
hai this is content in first file
mohammedrafi@NOC-RAFI:~/inode$ ls -li
total 4
21366866 -rw-rw-r– 1 mohammedrafi mohammedrafi 35 Oct 14 08:33 first
mohammedrafi@NOC-RAFI:~/inode$ ln first second
mohammedrafi@NOC-RAFI:~/inode$ ls -li
total 8
21366866 -rw-rw-r– 2 mohammedrafi mohammedrafi 35 Oct 14 08:33 first
21366866 -rw-rw-r– 2 mohammedrafi mohammedrafi 35 Oct 14 08:33 second
mohammedrafi@NOC-RAFI:~/inode$ stat first
File: ‘first’
Size: 35 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 21366866 Links: 2
Access: (0664/-rw-rw-r–) Uid: ( 1000/mohammedrafi) Gid: ( 1000/mohammedrafi)
Access: 2016-10-14 08:32:48.903699761 +0530
Modify: 2016-10-14 08:33:04.315699306 +0530
Change: 2016-10-14 08:33:32.915698461 +0530
Birth: –
mohammedrafi@NOC-RAFI:~/inode$ stat second
File: ‘second’
Size: 35 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 21366866 Links: 2
Access: (0664/-rw-rw-r–) Uid: ( 1000/mohammedrafi) Gid: ( 1000/mohammedrafi)
Access: 2016-10-14 08:32:48.903699761 +0530
Modify: 2016-10-14 08:33:04.315699306 +0530
Change: 2016-10-14 08:33:32.915698461 +0530
Birth: –
mohammedrafi@NOC-RAFI:~/inode$ rm -f first
mohammedrafi@NOC-RAFI:~/inode$ ls -li
total 4
21366866 -rw-rw-r– 1 mohammedrafi mohammedrafi 35 Oct 14 08:33 second
mohammedrafi@NOC-RAFI:~/inode$ cat second
hai this is content in first file
mohammedrafi@NOC-RAFI:~/inode$ readlink second
————————————
mohammedrafi@NOC-RAFI:~/inode$ cat >third
this is third file for soft-link test
mohammedrafi@NOC-RAFI:~/inode$ ls -li third
21366867 -rw-rw-r– 1 mohammedrafi mohammedrafi 38 Oct 14 08:36 third
mohammedrafi@NOC-RAFI:~/inode$ ln -s third fourth
mohammedrafi@NOC-RAFI:~/inode$ ls -li third fourth
21366868 lrwxrwxrwx 1 mohammedrafi mohammedrafi 5 Oct 14 08:37 fourth -> third
21366867 -rw-rw-r– 1 mohammedrafi mohammedrafi 38 Oct 14 08:36 third
mohammedrafi@NOC-RAFI:~/inode$ rm -f third
mohammedrafi@NOC-RAFI:~/inode$ ls -li third fourth
ls: cannot access third: No such file or directory
21366868 lrwxrwxrwx 1 mohammedrafi mohammedrafi 5 Oct 14 08:37 fourth -> third
mohammedrafi@NOC-RAFI:~/inode$ cat fourth
cat: fourth: No such file or directory
mohammedrafi@NOC-RAFI:~/inode$ readlink fourth
third
———————————
Hard and soft link for Directories
mohammedrafi@NOC-RAFI:~/inode$ mkdir dir1
mohammedrafi@NOC-RAFI:~/inode$ touch dir1/{a,b,c}
mohammedrafi@NOC-RAFI:~/inode$ ls -li dir1/
total 0
21366871 -rw-rw-r– 1 mohammedrafi mohammedrafi 0 Oct 14 09:00 a
21366875 -rw-rw-r– 1 mohammedrafi mohammedrafi 0 Oct 14 09:00 b
21366876 -rw-rw-r– 1 mohammedrafi mohammedrafi 0 Oct 14 09:00 c
mohammedrafi@NOC-RAFI:~/inode$ ln dir1/ dir2
ln: ‘dir1/’: hard link not allowed for directory
mohammedrafi@NOC-RAFI:~/inode$ ln -s dir1/ dir2
mohammedrafi@NOC-RAFI:~/inode$ ls -li dir2/
total 0
21366871 -rw-rw-r– 1 mohammedrafi mohammedrafi 0 Oct 14 09:00 a
21366875 -rw-rw-r– 1 mohammedrafi mohammedrafi 0 Oct 14 09:00 b
21366876 -rw-rw-r– 1 mohammedrafi mohammedrafi 0 Oct 14 09:00 c
mohammedrafi@NOC-RAFI:~/inode$ cat >dir1/a
this is file on dir1 which has soft link
mohammedrafi@NOC-RAFI:~/inode$ cat dir1/a
this is file on dir1 which has soft link
mohammedrafi@NOC-RAFI:~/inode$ cat dir2/a
this is file on dir1 which has soft link
mohammedrafi@NOC-RAFI:~/inode$ rm -f dir1/a
mohammedrafi@NOC-RAFI:~/inode$ cat dir2/a
cat: dir2/a: No such file or directory
mohammedrafi@NOC-RAFI:~/inode$ ls -li dir2/
total 0
21366875 -rw-rw-r– 1 mohammedrafi mohammedrafi 0 Oct 14 09:00 b
21366876 -rw-rw-r– 1 mohammedrafi mohammedrafi 0 Oct 14 09:00 c

One thought on “hard-soft links for files and directories”