When to use hard or softlinks
I would like to know the advantages of hard and symbolic links,
and when to use each.
If I make a backup of a directory, with more than 1 hard link to a file, will the contents of the file be copied more than once?
I use mostly rsync for backing up to another drive; sometimes K3b and cp to backup up some things to a cd.
Thanks for advice.
Someone may correct me here, but a hardlink points directly to a file and is indistinguishable from any other directory listing for that file. Hardlinks can only exist on the same filesystem (for most purposes equivalent to a disk partition) Say you have a file named "example" and create a hard link to it:
$ ln example examplelink
If example is, say a text file, the "file" command will report something like:
$ file example
example: ASCII text
$ file examplelink
example: ASCII text
If you "rm example", "examplelink" will still exist and refer to exactly the same file; The same data within the file is still all there and can be accessed using the name "examplelink" It can even be renamed back to the original name "example" or a new hardlink named "example" can be made.
A symbolic link (symlink) is more like an indirect pointer. It can be thought of as pointing to a directory entry for another file, rather than the file itself. A symlink can point to a file on another filesystem. If you create a symbolic link using the names above,
$ ln -s example examplelink
and then "rm example", then the file has really been removed. "examplelink" is now a broken link. If you use the "file" command now
$ file examplelink
examplelink: broken symbolic link to `example'
Trying to access the file now using "examplelink" will generate an error, since the file does not actually exist anymore.
Most of the time a symbolic link will be what is desired. There is a lot of flexiblility in using them, look up the /etc/alternatives system for an example. It is also used extensively in system startup scripts. To answer your 2nd question, yes, it would be copied twice, so could a symlink; there are options to the handle links in different ways for different copying commands.
qq .. then what is diff between hardlink and a copy ?
one i can think of is that inode will be same in case of hardlink but not in a copy
A hardlink is magic - it behaves exactly like the file linked to. A file will NOT be deleted until all hardlinks are deleted. In contrast, you can delete the file which a softlink points to.
So with a hardlink you can do something like this:
echo "Hello World" > orig/blahblah
ln orig/blahblah notorig/halbhalb
> Hello World
Try that with a soft link and you get:
> cat: notorig/halbhalb: No such file or directory
There is no "magic" to a hardlink. Any file (a bucket of bits) my be referenced by any number of (hard) links. When the last link is removed, storage for the referenced file is removed as well, freeing up disk space.
A symbolic (aka: soft) link is a named reference to a file. When the file referenced is removed, as above, storage is freed, leaving a dangling symbolic link.
Think of (hard) links as named references to a given file in the file system, each indistinguishable from the other.
Think of symbolic links as files that *contain the name of* some other file, and the OS hides this from you the user.
Use a hard link when you want an alternate name for the same bucket of bits within the same file system.
Use a soft link when you want to refer to another file *anywhere* in the directory tree by some given name.
You cannot create hard links to directories - this creates cycles when traversing the directory tree, and was forbidden long ago.
A directory entry contains the file name and a link to the inode structure for the file. A hard link creates another directory entry. The first directory entry is also a link. In other words, hard link <=> directory entry.
Create a hard link to a file and look at "ls -l". One of the fields is the number of links to a file.
Did you ever notice that a directory has at least two links? That is because the first one is the directory entry of the parent directory. The second is the "." entry in the directory itself. Since the parent directory contains a subdirectory, it will have a link count of at least 3. One for it's own "." entry. One for it's parents directory entry. One for the subdirectories ".." entry.
Look at the inode yourself for:
ls -ilad .
ls -ild Documents
The inode number will be the same.
|All times are GMT -5. The time now is 11:07 PM.|