Get to know Linux: Links
During your time using Linux you are going to run into what is called a link. Links are used quite a bit in the world of Linux. Most of the time they go completely unnoticed. There are times, however, when the user will have to create a link. One common link a user will create is a link pointing to the global Firefox java plugin. Because the user can't directly access that file in /usr/lib/mozilla/plugins a link must be created.
Links are very useful tools for a number of reasons. One very good use of Links is linking to a directory on another drive so you can easily access its contents. Say, for example, you have a hard drive containing mp3 files that is mounted to the directory /data. You want to be able to easily access those mp3 files but you don't always want to have to navigate through the directory structure to get to them. Make this easy with a link in your ~/ (home) directory.
Types of links
There are two types of links: Hard and Soft (Symbolic) links. The primary difference between hard and soft links is that hard links can only link to a file and can not span drives or volumes. Soft links, however, can link to directories and can span drives/volumes. At first you may think "Why even use hard links?" One major advantage to hard links is that a link will remain even if the original file is moved.
Another difference between hard and soft links, that isn't really examined by the end user is that hard links reference an exact inode whereas soft links reference abstract files/directories and are given their own, unique inode.
How to create links
Links are created by using the ln command. Hard links are created by using the ln command alone where soft links are created with the ln command using the -s switch.
Let's start out by creating a hard link. We'll use a very simple example. Using the Enlightenment E16 window manager requires you to edit the ~/.e16/menus/user_apps file to add to your menu. Let's say you want a link in your ~/ directory to that file so you're not always having to type so much to get to that file. You can do this with a hard link by entering the following command (as your standard user:
ln ~/.e16/menus/user_apps ~/user_apps
This will create a link in your home directory called user_apps. The nice thing about this is any time you edit either file, both will change. So you can simply edit the file in your home directory and the changes will reflect in your ~/.e16/menus/user_apps file.
One of the more common uses (as stated earlier) is linking a file from the /usr/lib/mozilla/plugins directory to your ~/.mozilla/firefox/XXX/plugins/ directory. Instead of linking file by file you can just link to the entire directory. NOTE: This is not often the case. Some distributions/installations handle the browser plugin system differently. I am using this as an obvious example of how soft links come in handy.
Where XXX is a random string of characters created upon installation of Firefox.
So to make a soft link from your /usr/lib/mozilla/plugins directory to your ~/.mozilla/firefox/XXX/ directory you would issue the command:
ln -s /usr/lib/mozilla/plugins ~/.mozilla/firefox/XXX/
Now your user installation of Firefox can see the plugins for the global installation. Of course this doesn't always work in the case of plugins. Again, I state it was used as an obvious example to explains links.
One of the nice aspects of soft links is that a standard user can link to files in any directory. But just because they can link to them doesn't mean they can edit them. You can create a link from the /etc/hosts.deny file to your home directory but, as the standard user, you can not edit the file. The standard user will, however, see all edits made to this file when edits are made by a user with write permissions.
Confused?
Let me try to explain that in another way. Say, for example, you want your users on your Linux machine to be able to see the contents of file /data/SAMPLE_DATA. You don't however want them to see the contents of the /data directory. Remove the read permissions from the directory /data with the command:
chmod -R o-w /data
and then give the file SAMPLE_DATA back read permission with:
chmod o+w /data/SAMPLE_DATA
command.
Now link to the SAMPLE_DATA file with the command:
ln /data/SAMPLE_DATA ~/
and the user will have a hard link to the file that they can read but not edit.
Final thoughts
Although links can be a bit confusing, they are a very helpful tool to use with the Linux operating system. They can make your administration job easier and save hard drive space. Get to know links, they are your friends.
Advertisement
I know this article is old, but this kind of bothered me.
This article is seemingly directed towards more novice users as an intro to links… then you go and say “hard links reference an exact inode whereas soft links reference abstract files/directories and are given their own, unique inode.”
It would be better info for the inteded audience to say something like “hard links create a duplicate index entry for the same file.” A user who does not know that a synbolic link is is certainly not going to know what an inode is.
Just a thought…
That was not so hard as i thought; links is like windows ‘shortcuts’.
Very interesting article. Thank you to clarify this Link stuff.