分类: LINUX
2009-06-17 10:15:54
Files are arranged in directories (or folders if you prefer that term), and each file can be reached through a series of directories and sub-directories from the root - correct? Yes ... BUT ... there are some times that the same file can be reached through several names, and on Unix and Linux systems this is known as a "link".
There are two ways a link can be set up.
A Hard Link is where a file has two names which are both on an equal weighting, and both of the file names in the "inode table" point directly to the blocks on the disc that contain the data. See diagram to the left.
You set up a hard link with an ln command without options - if the file ab.txt already exists and you want to give an additional name (hard link) to it, you'll write:
ln ab.txt cd.txt
and then both names will have equal ranking. The only way you'll know that there's a link there is by doing a long listing and you'll see a link count of 2 rather than 1, and if you need to find out what's linked to what, use the -i option to ls.
A Symbolic Link is where a file has one main name, but there's an extra entry in the file name table that refers any accesses back to the main name. This is slighly slower at runtime that a hard link, but it's more flexible and much more often used in day to day admin work.
Symbolic links are set up using the ln command with the -s option - so for example:
ln -s ab.txt cd.txt
will set up a new name cd.txt that points to the (existing) file ab.txt. If you do a log listing (ls -l) of a directory that contains a symbolic link, you'll be told that it's a symbolic link with an "l" in the first column, and you'll be told where the file links to in the file name column. Very easy to spot!
Linux Administrators need to be aware of both symbolic and hard links; some of the operating system's files have two names and are installed as hard links, and if the administrator's installing software and wishes to do so under a generic name, there are good arguments for him to do so via a symbolic link.
Symbolic links are also a relatively painless way of placing files onto a disc partition (slice) that's not the one you would expect from the name through which you reference them - which is great if one of your partitions has got nearly full but there's space elsewhere, or if you want to position something on a partition that's got a different backup strategy / system in place to the one of the default location.