Archive for the ‘3 – Hard and soft links’ Category

III – Hard and soft links

torsdag, juni 10th, 2010

    Before moving on I’d like to stress two more things:
1. Vengeance is a dish best served cold
2. Hard and soft links

Hard links

    A hard link is two or more file names referring to the same inode. Hard links share the same data blocks on the hard drive, while they continue to behave as independent files. Obviously you didn’t get that. Obviously I’ll give you an example:

<====================

#!/bin/bash
# Don't worry about the line above - I'll explain that later.

# First create a regular file
echo "one hard link ordered" > HLink_org.txt

# Now make a hard link to this file
ln HLink_org.txt HLink_link.txt

# Check new file
cat HLink_link.txt # This will provide an output like the original ‘one hard link ordered’

# List the files and their iNodes
ls -il
# This will provide an output similar to:
# 1482256 -rw-r–r– 2 sgar group 21 May 5 15:55 HLink_org.txt
# 1482256 -rw-r–r– 2 sgar group 21 May 5 15:55 HLink_link.txt
# As you can see both files refer to the same iNode (the number first in each row). If you change ‘HLink_org.txt’ the changes will also apply to
‘HLink_link.txt’

# add ‘appending class’ to file ‘HLink_org.txt’
echo ”appending class” >> HLink_org.txt

# Even though the changes were made to the original file they are found in ‘HLink_link.txt’ as well
cat HLink_link.txt

# If we remove the original file ‘HLink_org.txt’ the hard link ‘HLink_link.txt’ is still accessable and unmodified
rm HLink_org.txt
cat HLink_link.txt

===================/>

    When there are no links referring to a point of data e.g. when all hard links have been removed together with the original file (and no process has the file open for access) the data is deallocated by the operating system.

    The major disadvantage with hard links is that they cannot span over partitions since the iNode numbers are uniqe only within a given partition. But there is a cure for this – soft links (also called ’symbolic links’ or ’symlinks’).

Soft links

    Instead of pointing to an iNode, soft links point to a specific file path. Therefore they can span over partitions. On the other hand, if the source file is deleted the link will be broken although still visible.

<====================

#!/bin/bash
# Don't worry about the line above - I'll explain that later.

# First create a regular file
echo "soft links - easy like Sunday morning" > SLink_org.txt

# Now make a symbolic link to this file which is done with the ‘-s’ parameter
ln -s SLink_org.txt SLink_link.txt

# Check new file
cat SLink_link.txt # This will provide an output like the original ’soft links – easy like Sunday morning’

# List the files and their iNodes
ls -il
# This will provide an output similar to:
# 1482256 -rw-r–r– 1 sgar group 21 May 5 15:55 SLink_org.txt
# 1482226 lrwxrwxrwx 1 sgar group 21 May 5 16:22 SLink_link.txt -> SLink_org.txt
# In difference from the hard links we can see that the iNode numbers for the two files are different. Also one can notice the ‘l’ char in the ‘lrwxrwxrwx’ sequence
# which is the sign of a symbolic link. Also, for the soft link we can see that it refers to the ‘SLink_org.txt’ file.

# Now add ’soft dogs’ to file ‘SLink_link.txt’
echo ”soft dogs” >> SLink_link.txt

# The modifications made to the soft link is visible also in the original file.
cat SLink_org.txt

# But now, here comes the grande finale, if you remove the original file the soft link will not be destroyed but point to a file that does not exist
rm SLink_org.txt
cat SLink_link.txt
# Output’s gonna be something like ‘cat SLink_link.txt: File not found’ even though the actual soft link is still found in the file system

===================/>