Linux Classes
Linux Training
* Linux Classes *

LINUX CLASSES - LINUX COMMANDS

Can I Control the World with File Linking?

You can link files together in two ways: with hard links or symbolic links.

Creating a Hard Link

The ln command lets a file on your disk be accessed with more than one file name by hard-linking a new file name to it. When you hard-link a file, you create a new file name and connect it with the original file. Hard-linking can let two users share the same file or provide a more convenient way of referencing files buried deep in a series of directories.

Here's an example. Suppose hermie and sigmund are working on the Top Secret Snowgun Project, and the formula for the Snowgun is stored in /home/hermie/projects/snowgun/formula.txt. Sigmund doesn't want to type that long, ugly file name every time he needs to update the file, so he creates a hard link to the file, like this:

ln /home/hermie/projects/snowgun/formula.txt sgformula

The preceding command links the file name sgformula to the file formula.txt contained at the end of that directory string. There's still only one copy of the snowgun formula (formula.txt) on the disk, but now sigmund can access it quickly with the name sgformula (assuming hermie gives him write permission to the file). Interestingly, if hermie deletes his projects/snowgun/formula.txt file, Linux will not remove the file from the file system because sigmund still has a link to it.

Think of the ln command as a way of creating a kind of nickname for a file. By the way, ther e is no parallel to this file linking concept in the DOS world--you'd have to create a second copy of the file and keep the various copies in synch manually to get a similar result.

Creating a Symbolic Link

The type of link we just created is called a hard link. There's another type called a symbolic link. Symbolic links (also called symlinks) work like hard links, but you can do a bit more with them. If you want to create a link to a directory (as opposed to a file), you must create a symlink. Symlinks are also required when linking to a file on a different disk partition or on a network.

To create a symbolic link, add the -s parameter to the ln command, like this:

ln -s /dev/fd0 A:

ln -s /etc/httpd/conf web

The first example allows you to access the floppy drive as drive A, just like in DOS, and the second creates a directory entry called web that can be used instead of /etc/httpd/conf.

Previous Lesson: Task Scheduling
Next Lesson: The Vi Editor

[ RETURN TO INDEX ]

Comments (most recent first)

thamer     (18 Aug 2010, 09:12)
I want to delete the link that i have created
what is the command that help me doing this.
mirza     (09 Aug 2010, 01:01)
hello,
The number that is shown in the second column of output of ll, for files in hard link, i want to know what is it for directories?
for example /etc show 128 in that column, what is this number? is this a hard link or what?
thank you
reema alnsirat     (31 Jul 2010, 10:27)
Thank you for this important and great information
Eric     (12 Jun 2010, 14:37)
Devendra: What you saw does not contradict the article. When "instrahu" deletes the file, his reference to the physical file is removed. From his perspective, it's gone. The file is not actually deleted because a second reference still exists, the hard link created by "db2inst1". If "db2inst1" deletes his link, that was the last reference to the physical file and the file is removed from the filesystem.

Barbara: I believe that answers your question also. :)
Matt     (29 Apr 2010, 05:20)
Great explanation...makes perfect sense and nice quick simple read. Thanks!!!!!!!
Barbara     (13 Apr 2010, 02:11)
In the hard link example, if Hermie deletes the file, Linux keeps it. What happens if Sigmund deletes the link? Is the file finally deleted?
Devendra     (12 Apr 2010, 00:29)
Hey I found opposite to whatever you are stating for hard links.I had a file "Proactive_monitoring.out" on user "instrahu" and created the hardlink from another user "db2inst1"

1. I was able to delete the file even after creating the hard link.
2. Even after deleting the file I was able to access that file from other user from where I created the hard link. This was only possible if a local copy exist of the linked file and yes I found it on the same path where I created the hard link.
Ravster     (08 Apr 2010, 00:12)
hey thanks had to link to my htdocs directory and you made my day..easy simple..fabulous!! thanks homes
barrank     (19 Mar 2010, 19:07)
@moez: Try 'man ln'.

Bob, this site is great!!!
moez     (22 Feb 2010, 06:56)
i want to ask you if there is more options for 'ln' command .
moez     (22 Feb 2010, 06:54)
hello,

thank you for this interesting course
suraj     (15 Feb 2010, 08:53)
great help
thanks

*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:
 

Ask Bob Rankin - Free Tech Support
<Send This Link to a Friend>         <Bookmark This Page>


Copyright © by Bob Rankin
All rights reserved - Redistribution is allowed only with permission.