Linking Files

Linux system allows you to have a file known by more than one name, while maintaining a single copy in the disk. Files are linked with ln command , which takes two filename as arguments.

So, to link the two files emp.lst with, say employee, you would use


ln emp.lst employee

After this command if you can observe from ls –li command , that the two files have the same I-node number. The irony is that the ln command itself is a link to the other files cp and mv. This means that there is only one file which performs all the three functions, depending on the invocation filename This is useful when we want to get at a file quickly from within different directories. Assume that our directory structure looks like this:

we could link the file to our home directory. If we are in the home directory, the command to do this is:

ln project1/workspace/tables/table1 mytable1

To create the link when we are in the tables directory, the command would have been:

ln table1 ~/mytable1

After issuing either of these commands, an ls command in our home directory would show an entry for mytable1. The long format of the same command would show 2 links for the file mytable1:

ls -l
-rw------- 2 jsmythe 6 Jul 4 14:23 mytable1

A long format listing of the file table1 would also show 2 links. What if a file called mytable1 had already existed in our home directory? Linux would let us know that a file by that name exists, and would not make the link. The effect of linking is that the file now has two names. We may call up the file by either name. Creating a link does not change the ownership, group, or permissions for a file. The inode number of two files are same.

Links are removed using the rm command. To continue the example above, the command

rm mytable1

Removes one link to the file table1 by removing the file mytable1. The file table1 itself, and its contents, still exists. Only when all the links to the file have been removed will the file itself be erased.

The links described in the sections above are hard links. In effect, making a hard link creates a standard directory entry just like the one made when the file was created. Hard links have certain limitations. Hard links cannot be made to a directory, only to files, and hard links cannot be made across file systems and disk partitions.

There is another kind of link, called a symbolic link. Symbolic links can span file systems, and can be made for directories.

ln -s project1/workspace linkdir

The -s option means that a symbolic link is created (instead of the default hard link). This command creates a symbolic link called linkdir which points to the directory called a workspace. When we list the contents of linkdir

ls linkdir
data tables

we see a listing of the contents (data and tables) We can learn more about the new directory by using the long format and directory options with the ls command:

ls -ld linkdir
l---------  1 staff  7 Jun 11 13:27 linkdir 

Symbolic links are shown with an arrow (->) in the name column at the right.

Subscribe For More Content