Unix Hard vs. Soft Links
An exploration of Unix symbolic (soft) and hard links. Commands used: ln(1), ls(1), and touch(1).
$ ls $ touch original $ ln original hardlink $ ln -s original softlink $ ls -li total 8 4891103 -rw-rw-r-- 2 jdoe jdoe 0 Aug 17 23:18 hardlink 4891103 -rw-rw-r-- 2 jdoe jdoe 0 Aug 17 23:18 original 4891112 lrwxrwxr-x 1 jdoe jdoe 8 Aug 17 23:18 softlink -> original $ touch four $ ln -s four longername $ ls -l four longername -rw-rw-r-- 1 jdoe jdoe 0 Aug 17 23:27 four lrwxrwxr-x 1 jdoe jdoe 4 Aug 18 11:55 longername -> four
Observations:
The hardlink file uses the same inode number as the original.
Both the hardlink and original are empty, and share the same file type (a hyphen).
The softlink file is not empty, and has a l instead of a hyphen in the file type column.
The softlink file has a different inode number than the hard linked file.
The size of the symbolic link file appears directly proportional to the length of the filename it points to.
ls -l somehow knows how to display the file the symbolic link points to.
Technorati Tags: Unix
Hard links may only exist within a single partition, as each new hard link is simply a new filename-to-inode entry in a directory file. In contrast, a symbolic link is a special Unix file type (), and contains a file path that points elsewhere. ls and other programs use the readlink(2) system call to determine what file a symbolic link file points to, and then other system calls to reach the actual file. The stat(2) and other system calls automatically follow symbolic links, making them largely transparent. stat(2) also details the various allowed file types, including “plain text” (regular) and symbolic link files:
#define S_IFMT 0170000 /* type of file */
#define S_IFIFO 0010000 /* named pipe (fifo) */
#define S_IFCHR 0020000 /* character special */
#define S_IFDIR 0040000 /* directory */
#define S_IFBLK 0060000 /* block special */
#define S_IFREG 0100000 /* regular */
#define S_IFLNK 0120000 /* symbolic link */
#define S_IFSOCK 0140000 /* socket */
#define S_IFWHT 0160000 /* whiteout */
In closing, the whiteout type has nothing to do with Snow Crash.