ln command (hard and symbolic links)

alex at crackpot.org alex at crackpot.org
Fri Nov 9 13:25:07 MST 2007


Quoting Darrin Chandler <dwchandler at stilyagin.com>:

> On Fri, Nov 09, 2007 at 11:38:17AM -0800, keith smith wrote:
>>
>> Hi everyone,
>>
>> I'm needing to share some files between two hosting accounts and am  
>>  looking at creating a few symbolic links.
>>
>> In reading the man page for ln it says it creates a hard link by   
>> default or I can use --symbolic to create a symbolic link.
>>
>> What is the difference between a hard link and a symbolic link and   
>> why would I want to use one over the other?
>>
>> Thanks a bunch in advance for your help.

- Read up on inodes and filesystems.  Knowing the difference between a  
file and an inode will help the hard/soft link distinction make more  
sense.
http://en.wikipedia.org/wiki/Inode
http://en.wikipedia.org/wiki/Filesystem

- When you 'ls -l' a file, the first column after the permissions mask  
is the number of hard links to the file.
alexd at pni-bt12814:~/metagmetric/tests/tmp/deltas$ ls -l
total 64
-rw-r--r-- 1 alexd alexd 2 2007-11-09 08:59 file1
-rw-r--r-- 1 alexd alexd 8 2007-11-09 08:59 file2

Each of those are normal files, 1 link to each.  If I 'ln file2  
file2-link', then you see :
alexd at pni-bt12814:~/metagmetric/tests/tmp/deltas$ ls -l file*
-rw-r--r-- 1 alexd alexd 0 2007-11-09 13:13 file1
-rw-r--r-- 2 alexd alexd 0 2007-11-09 13:13 file2
-rw-r--r-- 2 alexd alexd 0 2007-11-09 13:13 file2-link

If I modify either file2 or file2-link, the operation is exactly the  
same.  I can remove file2 without affecting file2-link.  Looking at  
inodes and inode counts is the only way to distinguish a hard link  
from a normal file.

- Symbolic links can't be resolved outside of a chroot.  Hard links can.


More information about the PLUG-discuss mailing list