Way back, the UNIX filesystem only supported a physical link. So a directory entry might have been
struct dirent {
unsigned long d_ino;
char d_name[30];
};
So if your directory contained
8539501 foo
8539502 bar
8539503 baz
8539502 paw
Then it should be clear that the files bar, inode=8539502
and paw inode=8539502
are the same file, i.e a Physical (or hard) link. If you list the directory you'll see something like
[k5054@azure foo]$ ls -li
total 0
8539502 -rw-r--r-- 2 k5054 k5064 0 Oct 13 06:31 bar
8539503 -rw-r--r-- 1 k5054 k5064 0 Oct 13 06:31 baz
8539501-rw-r--r-- 1 k5054 k5064 0 Oct 13 06:31 foo
8530502 -rw-r--r-- 2 k5054 k5064 0 Oct 13 06:31 paw
The -i
flag to ls add the inode, and so we can see bar and paw have the same inode (8359502). Also, column 3 shows the number of physical links to the inode, which for bar and paw are 2, but foo and baz are only one. Note that a hard link doesn't need to reside in the same directory, you can for example do something like
ln foo/bar/baz ping/pong/paw
, which will create the physical link. There are 2 restrictions to hard links: 1) you can't create a hard link between directories, and 2) you can't create hard links across file systems. Symbolic (soft) links solve both these problems. In the case of a soft link, the link can be thought of as a reference. If we take the above directory and create a soft link between foo and bang via ln -s foo bang we now get: k5054@azure foo]$ ls -li total 4 8539536 lrwxrwxrwx 1 k5054 k5064 3 Oct 13 06:48 bang -> foo 8539502 -rw-r--r-- 2 k5054 k5064 0 Oct 13 06:31 bar 8539503 -rw-r--r-- 1 k5054 k5064 0 Oct 13 06:31 baz 8539501 -rw-r--r-- 1 k5054 k5064 0 Oct 13 06:31 foo 8539502 -rw-r--r-- 2 k5054 k5064 0 Oct 13 06:31 paw This shows that the file `bang` is a symbolic link (file type "l") which points to "foo". Now "bang" acts as a pointer or an indirection. In order to get to the contents of , the OS has to open bang, read its contents and then open the file pointed to by the contents. Also, note that the size of bang is 3, ie "foo". Since we now have this indirection, we can link to files in other filesystems, and we can also create links to other directories. You're also not limited to only one indirection. A soft link can point to another soft link, to another soft link, etc. "A little song, a little dance, a little seltzer down your pants" Chuckles t