Ln

From CLFS-HINTS

Jump to: navigation, search

Ln is used to make links to files. There are 2 types of links.

Contents

The Basics of how a *nix File System Works.

When a file is created, it is made in 2 parts. The first part is the filename and the location where the data and stuff is. The second part is where the file and the other data like permissions, etc are. The second part is called the inode.

When a file is created, The system finds an empty inode, and then creates the file permissions ans etc in that inode space. Then, in the file system's "table of contents," the filename and the inode location are entered. In this way, when a file is called, it points to the location of the data, etc.

Hard Links

A hard link is 2 filenames that point to the same inode. In this, the same data can be accessed by either the original filename, or by the new hard link. To a point, they are the same. If the original file is deleted, the inode remains because the hard link points to it. When an inode is no longer linked to anything, it is removed with the last filename deletion. There is always some exceptions though...

Both files have the same permissions, and if a permission or the data change under one filename, the changes are seen when assessing the other filename.

Soft Links

A soft link is a link that points to the original file, almost like a "Windows" shortcut. It points the user to where the original file is located. It has its own file permissions.

Working Example

This is an mewly created directory.

david@quandary:~/ln$ ls -lia
total 8
1929963 drwxrwxr-x  2 david david 4096 Jul 22 18:09 .
1929537 drwxr-xr-x 13 david david 4096 Jul 22 18:09 ..
david@quandary:~/ln$ 

When a folder is created, it is given a inode address. This is where the folder's permissions and other data are stored. The first hard link is to it's self. (.) The second hard link (..) is to its parent folder.

Lets create a simple file.

echo "Goodbye Crule World" > bob.txt

The result is :

david@quandary:~/ln$ ls -lia
total 12
1929963 drwxrwxr-x  2 david david 4096 Jul 22 18:14 .
1929537 drwxr-xr-x 13 david david 4096 Jul 22 18:09 ..
1929966 -rw-rw-r--  1 david david   20 Jul 22 18:15 bob.txt
david@quandary:~/ln$ 

Now, lets make a hard link to bob.txt

ln -v bob.txt hardbob.txt
david@quandary:~/ln$ ln -v bob.txt hardbob.txt
`hardbob.txt' => `bob.txt'
david@quandary:~/ln$  

Lets see what it looks like now.

david@quandary:~/ln$ ls -lia
total 16
1929963 drwxrwxr-x  2 david david 4096 Jul 22 18:16 .
1929537 drwxr-xr-x 13 david david 4096 Jul 22 18:09 ..
1929966 -rw-rw-r--  2 david david   20 Jul 22 18:15 bob.txt
1929966 -rw-rw-r--  2 david david   20 Jul 22 18:15 hardbob.txt
david@quandary:~/ln$  

We ca see in the first column that both bob.txt and hardbob.txt have the same number. (This is the inode number)

Lets make a soft link.

ln -sv bob.txt softbob.txt
david@quandary:~/ln$ ln -sv bob.txt softbob.txt
`softbob.txt' -> `bob.txt'
david@quandary:~/ln$  

And the result:

david@quandary:~/ln$ ls -lia
total 16
1929963 drwxrwxr-x  2 david david 4096 Jul 22 18:19 .
1929537 drwxr-xr-x 13 david david 4096 Jul 22 18:09 ..
1929966 -rw-rw-r--  2 david david   20 Jul 22 18:15 bob.txt
1929966 -rw-rw-r--  2 david david   20 Jul 22 18:15 hardbob.txt
1929967 lrwxrwxrwx  1 david david    7 Jul 22 18:19 softbob.txt -> bob.txt
david@quandary:~/ln$

We can see from the inode column that the the softbob.txt file has a different inode, and that it shows what the file is soft linked to.

I hope this helps with understanding links.

{Ideas and concepts came from"The Linux Gazette" "http://linuxgazette.net/105/pitcher.html"}

Personal tools