Tuesday, January 27, 2009

symbolic links Vs hard Links

sulekha

AFAIK the following are the differences between symbolic links and hard links Is there any other point which I have missed ?

symbolic link

when you try to open a symbolic link which points to a file or change to one that points to a directory,the command you run acts on the file or directory that is the target of that link.

The target has its own permissions and ownership that you cannot see from the symbolic link.

The symbolic link can exist on a different disk partition than the target.

Hard link

It can only be used on files(not directories) and is basically a way of giving multiple names to the same physical file.

hard links that point to that single physical file must be on the same partition as the orginal target file.

The files are hard links if they have the same inode number.


Grant

Not always, consider NFS ;)

When using hard linked file trees, some tools (patch) know how to break hardlinks when required. Also means using an editor that is aware of hardlinks and knows to break them when modifying a file. I use hard links for linux-kernel source trees (cp -al kernel-a kernel-b), also
for backups via a cron job where only changed files are copied whilst older files are simply hard-linked, takes much less space and is far more convenient than incremental backup methods. Rsync is hardlink aware.


Andrew Halliwel


Fairly correct apart from one thing.You CAN hardlink a directory.in fact, you see those "." and ".." directories in your directory listing when you ls -a?

Them's hard links them are. Do an ls -ia on a directory and compare the inode number for ".." with the inode of the parent directory. Then compare the inode number of the "." with the inode of the directory you're in.


maxwell lol

Well, the OS can do this. The user has no control.

Older versions of Unix allowed this, but when you ran fsck, the file system would be corrupted, as I recall.

So mkdir became an atomic operation.....


Andrew Halliwel

Heh, true. I'd never tried hard linkin a directory.Just assumed it was possible cos of the dot hardlinks.


kees theunissen

And the OS does this only for the special directories "." and "..". Allowing multiple hard links, other than "." and "..", for directories would imply that directories could have multiple parent directories. This would break the concept of ".." pointing to _the_ parent directory.


billymayday

http://lwn.net/Articles/294667/


musafi


How a Hard link is created?

A hard link to a file is created using the following command

root@xyz# ln file1 file2

The above command makes file2 a hard link to a file represented by the hard link file1.

How a Soft link is created?

The soft link to a file is created using the following command

root@xyz# ln -s file1 file3

The above command makes file3 a soft link to a file represented by the hard link file1

How a file with multiple hard links is deleted?

A file with multiple hard links is only deleted after all its hard links are deleted. Then the question is, Is there any way that we can remove all of the hard links in a single command? The answer to this question is ...*

How a file with a soft link is deleted?

The file with a soft link is deleted normally by using the rm command. In this case the soft link becomes broken, i.e. not linked to anything.

What happens to the soft links if a file is moved from its original location?

In case a file is moved from its original location the soft link pointing to this file becomes broken. If the pointed file is moved back to its original location, the broken link becomes active again.

What happens to the soft links if a file is removed?

If a file is removed the soft links continue to exists but becomes broken.

What happens to the hard links a file is moved from its original location?

It makes no difference. By moving a file having multiple hard links means moving a hard link. So if a hard link is moved to some other place, it does not effect any other hard link pointing to the same file.


What happens to the hard links if a file is removed?

It just is not possible to remove a file having at least one hard link. If you remove one hard link, that particular link is deleted not the actual file. The actual file is only deleted when the last hard link pointing to the file is removed.

How can we list all of the hard links or soft links to a particular file?

List Hard Links

You can list all of the hard links to a file 'file1' by using the following command

root@xyz# find [directory to search] -samefile [file name (which can be any hard link) as argument]

Ex: root@xyz# find / -samefile file1 or root@xyz# find . -samefile file1

You can also use the inode number for searching the hard links to it

Ex: root@xyz# find . -inum [inode number]

You can find the inode number of a file by using the following command

root@xyz# ls -i

List Soft Links

You can list the soft links to a file using the following command

root@xyz# find -lname file1

It is better to put a * as prefix in the file name like below

root@xyz# find -lname "*file1"

Because we have not mentioned the directory where the search should be made so the search will be made in the current directory. We can mention the directory where the search should be made in the following way

root@xyz# find . -lname "*file1" or root@xyz# find / -lname "*file1"



see also:

http://linuxgazette.net/105/pitcher.html

No comments:

Post a Comment