How To Discovery As Well As Delete All Difficult Links To A File

Deleting a file is deceptively simple. You tin but role the rm ascendance similar this.
$ rm file1


However, if the file has 1 or to a greater extent than hard links to it, life gets to a greater extent than interesting. You involve to attempt together with destroy all difficult links to the file.

A difficult link is essentially around other get upwards for a file. Hard links to file1 tin move created equally follows:
$ ln file1 file2
$ ln file1 tmp/file3


file2 together with file3 cash inwards one's chips around other get upwards of file1.

How produce I know if around file has a difficult link to it?

Do a long listing of the file similar this.
$ ls -l file1
-rw-r--r-- three peter peter 0 2008-09-01 16:15 file1


Note that the difficult link count has the value three (this is the set out to the left of the file owner). It agency that the physical file has three names: file1 together with 2 others.

If yous entirely rm file1, the other 2 names yet exist, together with users tin yet access the physical file using these 2 names.

To honor out all difficult links to file1, role this command.
$ honor /home -xdev -samefile file1
/home/peter/file1
/home/peter/tmp/file3
/home/peter/file2


Note that nosotros specified /home equally the search starting point. You should supplant /home amongst the mountain indicate for the filesystem inwards which your file1 was created. (This is probable to move either /home or / for files inwards your habitation directory.)

The argue for using the filesystem's mountain indicate is that difficult links are restricted to reside inwards the same filesystem equally the physical file. This agency that file2 together with file3 must move inwards the same filesystem equally file1. Therefore, to honor ALL difficult links to a file, yous must start search at the mountain indicate of the filesystem.

The -xdev selection instructs find NOT to traverse directories inwards other filesystems.

To honor together with delete all difficult links to a file:
$ honor /home -xdev -samefile file1 | xargs rm


An option (and slightly to a greater extent than complicated) method is yesteryear using inodes. An inode is a information construction assigned to a physical file that contains all its meta-information except the file's name(s).

All difficult links to a physical file portion the same inode, together with convey the same inode id number. Therefore, if yous know the inode set out of a file, yous tin honor all difficult links to the file yesteryear searching for files amongst the same inode number.

To display a file's inode number, role ls -i:

$ ls -li file1
2655341 -rw-r--r-- three peter peter 0 2008-09-02 19:09 file1


The get-go column inwards the higher upwards listing shows the file's inode number.

Now that yous know the inode set out of file1, yous tin role the find ascendance to search for all files that convey the same inode number:

$ honor /home -xdev -inum 2655341
/home/peter/file1
/home/peter/tmp/file3
/home/peter/file2


To delete all difficult links to the physical file:
$ honor /home -xdev -inum 2655341 | xargs rm 

Berlangganan update artikel terbaru via email:

0 Response to "How To Discovery As Well As Delete All Difficult Links To A File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel