Loading...

Thursday, October 2, 2008

Hard or Soft link

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]

for example

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

for example

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"


For more details you can have a look at the following resources

  1. Hard and Symbolic link [A free registration will be required for the above link]
  2. Symbolic link
  3. Hard link

1 comment:

Julian Pierce said...

This is how I would remove all hardlinks and the target file "file1":

username> find / -samefile file1 -exec rm {} \;

Free Advertising