To create a hard link we just use the same semantics as with the copy command. we make a link from a source name to a destination name. Let's create a hard link:
$ls -Fl total 3 -rw------- 1 markst staff 0 Jan 21 17:13 a -rw------- 1 markst staff 0 Jan 21 17:07 b -rw------- 1 markst staff 0 Jan 21 17:07 c drwx------ 3 markst staff 1024 Feb 6 20:47 data/ drwx------ 2 markst staff 1024 Jan 21 17:11 junk/ drwx------ 2 markst staff 1024 Feb 6 21:16 programs/ $ln a d $ls -Fl total 3 -rw------- 2 markst staff 0 Jan 21 17:13 a -rw------- 1 markst staff 0 Jan 21 17:07 b -rw------- 1 markst staff 0 Jan 21 17:07 c -rw------- 2 markst staff 0 Jan 21 17:13 d drwx------ 3 markst staff 1024 Feb 6 20:47 data/ drwx------ 2 markst staff 1024 Jan 21 17:11 junk/ drwx------ 2 markst staff 1024 Feb 6 21:16 programs/ $
The first directory listing shows all files having only one name refering to them. The second listing shows the file with the name 'a' has two hard links to it (including the name 'a' itself), and the file 'd' also has two hard links to it. We cannot however see which other filenames point to them. there is a way however to find this out. Unix assigns every file an inode, which stores the size, location, number of hard links and other information of a file, and is uniquely identified by an inode-number. Directories are just listings of names and associated inode-numbers. To find a file Unix looks up the inode-number associated with the filename requested, finds the inode and from there finds the location. So if we want to know what the other name of a file is, we just have to list all the directories with inode-numbers and find the other name with the same inode-number. This is a very laborious process, which make hard links unattractive.
Another piece of information we can gain from the directory listing is the fact that directories also have hard links to them. junk and programs only have two hard links, but data has three. Each of the directories has a file called '.' in itself which is a hard link to itself. This is not normally shown by ls, but can be forced by the -a (for all) flag on ls. The data directory however has a third link to it; remeber the data directory has a subdirectory called personal. This subdirectory contains a file called '..' which is a hard link to data. this enables us to 'go up' one level in the directory structure.
Now let us look at the directory in full:
$ls -Fla total 5 drwx------ 5 markst staff 1024 Mar 4 20:17 ./ drwx------ 4 markst root 1024 Jan 21 17:07 ../ drwx------ 2 markst staff 1024 Feb 6 21:16 programs/ drwx------ 3 markst staff 1024 Feb 6 20:47 data/ drwx------ 2 markst staff 1024 Jan 21 17:11 junk/ -rw------- 2 markst staff 0 Jan 21 17:13 a -rw------- 1 markst staff 0 Jan 21 17:07 b -rw------- 1 markst staff 0 Jan 21 17:07 c -rw------- 1 markst staff 0 Jan 21 17:52 .a -rw------- 2 markst staff 0 Jan 21 17:13 d $