Monday 21 October 2013

ELA_17_Managing_File_Links

Links let you give a single file more than one name. Files are actually identified by the system by their inode number, which is just the unique file system identifier for the file. A directory is actually a listing of inode numbers with their corresponding filenames. Each filename in a directory is a link to a particular inode.

Hard links.

The ln command is used to create multiple links for one file. For example, let's say that you have a file called foo in a directory. Using ls -i, you can look at the inode number for this file.


/home/larry# ls -l foo
22192 foo
/home/larry#

Here, foo has an inode number of 22192 in the file system. You can create another link to foo, named bar, as follows:


/home/larry# ln foo bar

With
ls -i, you see that the two files have the same inode.



/home/larry# ls -l foo bar
22192 bar 22192 foo
/home/larry#

Now, specifying either
foo or bar will access the same file. If you make changes to foo, those changes appear in bar as well. For all purposes, foo and bar are the same file.
These links are known as hard links because they create a direct link to an inode. Note that you can hard-link files only when they're on the same file system; symbolic links (see below) don't have this restriction.
When you delete a file with rm, you are actually only deleting one link to a file. If you use the command



/home/larry# rm foo

then only the link named
foo is deleted, bar will still exist. A file is only truly deleted on the system when it has no links to it. Usually, files have only one link, so using the rm command deletes the file. However, if a file has multiple links to it, using rm will delete only a single link; in order to delete the file, you must delete all links to the file.
The command ls -l displays the number of links to a file (among other information).



/home/larry# ls -l foo bar
-rw-r—r-- 2 root root 12 Aug 5 16:51 bar
-rw-r—r-- 2 root root 12 Aug 5 16:50 foo
/home/larry#

The second column in the listing, ``
2'', specifies the number of links to the file.
As it turns out, a directory is actually just a file containing information about link-to-inode associations. Also, every directory contains at least two hard links: ``.'' (a link pointing to itself), and ``..'' (a link pointing to the parent directory). The root directory (/) ``..'' link just points back to /. (In other words, the parent of the root directory is the root directory itself.)

Symbolic links.

Symbolic links, or symlinks, are another type of link, which are different from hard links. A symbolic link lets you give a file another name, but doesn't link the file by inode.
The command ln -s creates a symbolic link to a file. For example, if you use the command
/home/larry# ln -s foo bar

you will create a symbolic link named bar that points to the file foo. If you use ls -i, you'll see that the two files have different inodes, indeed.

However, using
ls -l, we see that the file bar is a symlink pointing to foo.
/home/larry# ls -l foo bar
lrwxwxrwx 1 root root 3 Aug 5 16:51 bar --> foo
-rw-r—r-- 1 root root 12 Aug 5 16:50 foo
/home/larry#



The file permissions on a symbolic link are not used (they always appear as rwxrwxrwx). Instead, the permissions on the symbolic link are determined by the permissions on the target of the symbolic link (in our example, the file foo).
Functionally, hard links and symbolic links are similar, but there are differences. For one thing, you can create a symbolic link to a file that doesn't exist; the same is not true for hard links. Symbolic links are processed by the kernel differently than are hard links, which is just a technical difference but sometimes an important one. Symbolic links are helpful because they identify the file they point to; with hard links, there is no easy way to determine which files are linked to the same inode.
Links are used in many places on the Linux system. Symbolic links are especially important to the shared library images in /lib. See page for more information.

No comments :

Post a Comment