17. Hard Links and Symbolic Links
Today we're going to test your virtual imagination ability! You're probably familiar with shortcuts in Microsoft Windows or aliases on the Mac. Linux has something, or actually some things similar, called hard links and symbolic links.

Symbolic links (also called symlinks or softlinks) most resemble Windows shortcuts. They contain a pathname to a target file. Hard links are a bit different. They are listings that contain information about the file. Linux files don't actually live in directories. They are assigned an inode number, which Linux uses to locate files. So a file can have multiple hardlinks, appearing in multiple directories, but isn't deleted until there are no remaining hardlinks to it. Here are some other differences between hardlinks and symlinks:

1. You cannot create a hardlink for a directory.
2. If you remove the original file of a hardlink, the link will still show you the content of the file.
3. A symlink can link to a directory.
4. A symlink, like a Windows shortcut, becomes useless when you remove the original file.

So far this is probably a bit tough to grasp, but stick around, we're going to explain it.


IMPORTANT: The tips in this document require the use of command-line commands. For more information about how to read and execute Linux command-line prompts and commands, please check the Linux Clues Linux Cheat Sheet, especially Linux Prompt Basics and Linux Command-Line Nomenclature. You'll need to start by logging in as root. If you're not sure how to do that, read Logging in and out as Root.


Let's do a little experiment to demonstrate the case. Make a new directory called Test and then move into it. to do that, type:

$ mkdir Test
$ cd Test

Then make a file called FileA:

$ vi FileA

Press the I key to enter Insert mode:


Then type in some funny lines of text (like "Why did the chicken cross the road?") and save the file by typing:


So, you made a file called FileA in a new directory called "Test" in your /home. It contains an old and maybe not so funny joke. Now, let's make a hardlink to FileA. We'll call the hardlink FileB.

$ ln FileA FileB

Then use the "i" argument to list the inodes for both FileA and its hardlink. Type:

$ ls -il FileA FileB

This is what you get:

1482256 -rw-r--r--     2 bruno bruno     21 May 5 15:55 FileA
1482256 -rw-r--r--     2 bruno bruno     21 May 5 15:55 FileB

You can see that both FileA and FileB have the same inode number (1482256). Also both files have the same file permissions and the same size. Because that size is reported for the same inode, it does not consume any extra space on your HD!

Next, remove the original FileA:

$ rm FileA

And have a look at the content of the "link" FileB:

$ cat FileB

You will still be able to read the funny line of text you typed. Hardlinks are cool.

Staying in the same test directory as above, let's make a symlink to FileB. Call the symlink FileC:

$ ln -s FileB FileC

Then use the i argument again to list the inodes.

$ ls -il FileB FileC

This is what you'll get:

1482256 -rw-r--r--     1 bruno bruno     21 May 5 15:55 FileB
1482226 lrwxrwxrwx    1 bruno bruno      5 May 5 16:22 FileC -> FileB

You'll notice the inodes are different and the symlink got a "l" before the rwxrwxrwx. The link has different permissions than the original file because it is just a symbolic link. Its real content is just a string pointing to the original file. The size of the symlink (5) is the size of its string. (The "-> FileB" at the end shows you where the link points to.)

Now list the contents:

$ cat FileB
$ cat FileC

They will show the same funny text.

Now if we remove the original file:

$ rm FileB

and check the Test directory:

$ ls

You'll see the symlink FileC is still there, but if you try to list the contents:

$ cat FileC

It will tell you that there is no such file or directory. You can still list the inode. Typing:

$ ls -il FileC

will still give you:

1482226 lrwxrwxrwx     1 bruno bruno     5 May 5 16:22 FileC -> FileB

But the symlink is obsolete because the original file was removed, as were all the hard links. So the file was deleted even though the symlink remains. (Hope you're still following.)

OK. The test is over, so you can delete the Test directory:

$ cd ..
$ rm -rf Test     (r stands for recursive and f is for force)

Note: Be cautious using "rm -rf"; it's very powerful. If someone tells you to do "rm -rf /" as root, you might loose all your files and directories on your / partition! Not good advice.

Now you know how to create (and remove) hardlinks and symlinks to make it easier to access files and run programs. See you on the links!