Example of using 'ln'
Posted: 6/9/2003 11:23:00 AM
By: Comfortably Anonymous
Times Read: 6,632
0 Dislikes: 0
Topic: Linux
Parent Message
Although the ln command has several options, in the last four years of using Linux heavily at work, I've only had reason to use 'ln -s' and 'ln' with no options. So unless you're trying to pull off something really different, that's really all you need to concern yourself with.

Using the '-s' option requires you to know the difference between a 'hard link' and a 'symlink':

A "hard link" is another name for an existing file; the link and the original are indistinguishable.  Technically speaking, they share the same inode, and the inode contains all the information about a file--indeed, it is not incorrect to say that the inode _is_ the file. On all existing implementations, you cannot make a hard link to a
directory, and hard links cannot cross filesystem boundaries.  (These restrictions are not mandated by POSIX, however.)

"Symbolic links" ("symlinks" for short), on the other hand, are a special file type in which the link file actually refers to a different file, by name.  When most operations (opening, reading, writing, and so on) are passed the symbolic link file, the kernel automatically "dereferences" the link and operates on the target of the link.  But some operations (e.g., removing) work on the link file itself, rather than on its target.  

In other words, a symlink is just a pointer to a file. Say you have a simlink /home/myname/somefile.txt which actually points to /home/yourname/textfiles/yourfile.txt. If you bring up /home/myname/somefile.txt in an editor, you are actually editing the /home/yourname/textfiles/yourfile.txt file, even though it looks to you like you are editing /home/myname/somefile.txt. If you have a symlink pointing to an executable file, then the executable file will run if you type the name of the symlink.

Here's the biggest and most dangerous difference between a hardlink and a symlink: If you delete a symlink, you only delete the pointer to the actual file. However, if you have a hard link /home/myname/hardlink.txt "pointing" to /home/myname/importantinfo.txt and you delete /home/myname/hardlink.txt, you ALSO DELETE /home/myname/importantinfo.txt!! Be careful, a hard link can burn you.

Weirdly, only the root user can create a symlink. Any user can create a hard link. I still haven't figured that one out. If someone knows why, please reply to this message and let me know! :)

Another bit of lore about ln -s:

To make a symlink from /home/myname/some/directory/pointerfile to /usr/local/something/configfile, it's a lot easier to change to the directory where the pointerfile is going to live before creating the link. Otherwise you have to play a lot of weird games to get the link name right.

For example:

cd /home/myname/some/directory
ln -s /home/myname/otherdir/configfile pointerfile

That will create a symlink pointing to /home/myname/otherdir/configfile in the directory /home/myname/some/directory called pointerfile.

However, if you try this instead:

cd /home/myname
ln -s ./some/directory/configfile ./otherdir/pointerfile

You'd think you'd end up with the same thing as in the first example. However, it doesn't work right. If you do an

ls -l ./otherdir

You will see a broken symlink pointing to a non-existent location of "./some/directory/configfile". Note that the current directory symbol './' is not interpreted, it is tacked on literally to the simlink name.

Enough for now, any questions, just reply to this message!
Rating: (You must be logged in to vote)
Discussion View:
Replies:

Example of using 'ln'
Posted: 6/9/2003 11:23:00 AM
By: Comfortably Anonymous
Times Read: 6,632
0 Dislikes: 0
Topic: Linux
"if you have a hard link /home/myname/hardlink.txt "pointing" to /home/myname/importantinfo.txt and you delete /home/myname/hardlink.txt, you ALSO DELETE /home/myname/importantinfo.txt!! Be careful, a hard link can burn you."

THAT'S NOT TRUE !!!
(I think)
Rating: (You must be logged in to vote)

Example of using 'ln'
Posted: 6/9/2003 11:23:00 AM
By: Comfortably Anonymous
Times Read: 6,632
0 Dislikes: 0
Topic: Linux
Whenever I try to set up a symbolic link using the ln -s command, it seems to create the link file ok, but if I try to navigate to this file (created in a normal user's web directory) via a browser I get a 'permission denied' message. I even tried to chown the symbolic file back from root to the user, same thing. Any ideas?
Rating: (You must be logged in to vote)