You are not logged in.

#1 2005-09-09 20:53:07

Wubbeneezer
Member
From: Northern California
Registered: 2005-03-12
Posts: 13

Quick symlink question

Hi, I've got a question about symlinks. I've already Googled a bit and searched the ln and symlink man pages.

Here's the question: let's say I have a file called /home/wubbeneezer/archie.doc. If I do ln -s (a soft link) to a file on a separate data partition (let's call the new file /data/archie.doc), do I have to copy the file from the old location to the new, or does ln take care of that? Also is the actual file information stored at /data/archie.doc or both the old and new locations?

Hope this isn't a stupid question. Man symlink says:
************************************************************************************
symlink creates a symbolic link named newpath which contains the string oldpath.

Symbolic links are interpreted at run-time as if the contents of the link had been substituted into the path being followed to find a file or directory.

Symbolic links may contain .. path components, which (if used at the start of the link) refer to the parent directories of that in which the link resides.

A symbolic link (also known as a soft link) may point to an existing file or to a nonexistent one; the latter case is known as a dangling link.

The permissions of a symbolic link are irrelevant; the ownership is ignored when following the link, but is checked when removal or renaming of the link is requested and the link is in a directory with the sticky bit set.

If newpath exists it will not be overwritten.
*************************************************************************************

My confusion stems from the fact that in my reading of the documentation, it is implied (without actually being explicitly stated) that the file information is moved to the new location. Please enlighten--thanks.


When all is said and done,
There's nothing left to say or do.

Offline

#2 2005-09-09 21:04:26

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Quick symlink question

Soft links (Symbolic) never copy the data at all. Its more like a pointer to the original file. Think of windows shortcuts.

Now, if you were talking about hard links, I have no idea what would happen. if you had a hard link to a file that was on a different partition, and then unmounted the file on the other partition, what happens. Maybe this is illegal...

Dusty

Offline

#3 2005-09-09 22:10:13

alterkacker
Member
From: Peoples Republic of Boulder
Registered: 2005-01-08
Posts: 52

Re: Quick symlink question

A hard link *cannot* point to a file on a different partition (i.e. filesystem). A hard link is simply an additional directory entry that points to the same inode as the original entry & inodes are unique only within a given filesystem. All hard links are equal - if you create a hard link & then delete the file using the original name the file is still there under the remaining hard link name; the file will only be truly deleted when the link count goes to zero.

A symbolic link on the other hand is simply a special type of file whose contents are the path to the original file. If you delete the original file the symlink is still there but is now orphaned, pointing to nothing.

Offline

#4 2005-09-09 22:29:59

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Quick symlink question

that's what I thought. Easy to test. I have my / and home directories on different partitions if I try to create a hard link to a file on / in my home directory it tells me I'm a bastard:

dusty:~ $ mount
/dev/sdb2 on / type reiserfs (rw)
none on /proc type proc (rw)
none on /sys type sysfs (rw)
none on /dev/pts type devpts (rw)
none on /dev/shm type tmpfs (rw)
tmpfs on /tmp type tmpfs (rw)
/dev/sda4 on /home type reiserfs (rw)
none on /proc/bus/usb type usbfs (rw)
dusty:~ $ ln /etc/rc.conf
ln: creating hard link `./rc.conf' to `/etc/rc.conf': Invalid cross-device link

Back to the original question, if you have a symlink on one device to a file on another device, the link connects to the original file. However, if the device containing the original file isn't mounted and you try to access the symlink, you will get an error.

Dusty

Offline

#5 2005-09-11 07:21:38

butters
Member
Registered: 2005-08-31
Posts: 35

Re: Quick symlink question

hardlink = fat entry pointing to file
softlink = file pointing to fat entry pointing to file

Every file automatically has one hardlink.  If you create a symlink to a hardlink to a file that has multiple hardlinks, and then you unlink the hardlink, then the symlink becomes orphaned even though the file still exists.

You can see the number of hardlinks to a filesystem node by using 'ls -l'.  The number to the left of the owner's username is the number of hardlinks.  For directory nodes there is some correlation between this number and the number of subdirectories within.  Each subdir has a special node '..' which is a hardlink to the parent directory and '.' which is a hardlink to itself.  Directories containing only regular files have 2 hardlinks: the directory name itself and the '.' hardlink within.

Because of their power, both hardlinks and symlinks create some pretty interesting scenarios.  Many tools have options to enable or disable the following of symlinks, and most people don't even understand how hardlinks work.

Offline

Board footer

Powered by FluxBB