Pages

Thursday, December 6, 2012

Symbolic link 101

bash-4.1$ stat sample
  File: `sample'
  Size: 5             Blocks: 68         IO Block: 65536  regular file
Device: 1bh/27d    Inode: 502918152   Links: 1
Access: (0600/-rw-------)  Uid: (97399/bruce)   Gid: (   34/ UNKNOWN)
Access: 2013-01-20 18:18:31.624217805 -0500
Modify: 2013-01-20 18:18:31.624217805 -0500
Change: 2013-01-20 18:18:55.152443117 -0500

bash-4.1$ ln -s sample sample1

bash-4.1$ stat sample
  File: `sample'
  Size: 5             Blocks: 68         IO Block: 65536  regular file
Device: 1bh/27d    Inode: 502918152   Links: 1
Access: (0600/-rw-------)  Uid: (97399/bruce)   Gid: (   34/ UNKNOWN)
Access: 2013-01-20 18:18:31.624217805 -0500
Modify: 2013-01-20 18:18:31.624217805 -0500
Change: 2013-01-20 18:18:55.152443117 -0500

So, the link count did not increase. If you don't use the -s flag and create a hard link, the link count increases by 1 on each link operation. 


Use the readlink command to read contents of a symbolic link


lrwxrwxrwx.  1 foo admin    5 Dec  6 23:21 avg_s.c -> avg.c


$ readlink avg_s.c
avg.c

So, the symbolic link contains the path to the file it is pointing to.


Soft link pros:
1) Can create link to files in other file systems or remote file systems.
2) Can create link to directories. Hard link to directories are not allowed in Unix.
3) Editing file using text editors like vim is more reliable. If we have hard links the edits to file inode are done in place make it prone to inconsistency on failures.
Cons:
1) Wastes an inode and takes space on disk.
2) Indirection. Slower.


Hard link pros:
1) Faster access as there is no indirection
2) Protection from accidental deletion.
3) Versioning/Backup software can easily make sure that file is backed up only once.


No comments:

Post a Comment