In order to better understand soft and hard links lets take a quick look at inode and what it does. Inode basically is metadata that holds files information in form of attributes like type, size, permissions, owners name, last access time, modification time, ACL settings. Inode also has pointers to the location in the file system where the file data is stored. The inode has unique numeric identifier that is used by kernel for accessing and managing the file.
To get a listing of an inode number, use the following command
#ls -i myfile.txt 2493606 myfile.txt
Stat command is used to display file statistics and also will displays inode number of a file
#stat myfile.txt File: ‘myfile.txt’ Size: 9 Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d Inode: 2493606 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ boris) Gid: ( 1000/ boris) Access: 2016-03-04 12:07:57.883470598 -0500 Modify: 2016-03-04 12:08:06.339423223 -0500 Change: 2016-03-04 12:08:06.339423223 -0500 Birth: -
Linking files or directories creates additional instances of them. The all point to the same physical data location. The difference between soft and hard links is weather linked files have same inode numbers and metadata.
Soft links also knows as symbolic links. They are very similar to Windows shortcuts.
- Each soft link has a unique inode number which stores metadat like path to the file it is linked with.
- Soft link can cross file system boundaries
- Soft links can link directories
In this example we will create soft link pointing to mydir/myfile.txt
#ln -s mydir/myfile.txt #ls -l total 4 drwxr-xr-x 2 root root 4096 Mar 4 12:26 mydir lrwxrwxrwx 1 root root 16 Mar 4 12:27 myfile.txt -> mydir/myfile.txt
If you look at inode number on soft link and actual file, you will see that they are different
#ls -li total 4 2493605 drwxr-xr-x 2 root root 4096 Mar 4 12:33 mydir 2493607 lrwxrwxrwx 1 root root 16 Mar 4 12:35 myfile.txt -> mydir/myfile.txt #cd mydir/ #ls -li total 4 2493606 -rw-r--r-- 1 root root 8 Mar 4 12:33 myfile.txt
Lets try to delete this file now and try to access soft link we created
#cd mydir/ #ls myfile.txt #rm myfile.txt #cd .. #ls -l total 4 drwxr-xr-x 2 root root 4096 Mar 4 12:29 mydir lrwxrwxrwx 1 root root 16 Mar 4 12:27 myfile.txt -> mydir/myfile.txt #cat myfile.txt cat: myfile.txt: No such file or directory
Unlike soft links hard links associate one or more files with a single inode number.
- Files indistinguishable from one another.
- Files have identical permission, ownership, time stamp
- Changes made to one file reflect in linked file also
- Hard link cannot cross file system boundaries
- Hard link cannot be used to link directories
Lets create hard link to myfile.txt located in mydir directory
Comparing inodes from original file and hard link we can tell that they are the same
#ls -li total 8 2493605 drwxr-xr-x 2 root root 4096 Mar 4 12:33 mydir 2493606 -rw-r--r-- 2 root toot 8 Mar 4 12:33 myfile.txt #cd mydir/ #ls -li total 4 2493606 -rw-r--r-- 2 root root 8 Mar 4 12:33 myfile.txt
Lest see what happens when we remove link
#rm myfile.txt #ls mydir #cd mydir/ #ls myfile.txt #cd .. #ls mydir #ln mydir/myfile.txt #ls mydir myfile.txt #cd mydir/ #ls myfile.txt #rm myfile.txt #ls #cd .. #ls mydir myfile.txt
As you can see in this example removing hard link does not remove original file and the other way around.