How to get all the hard links that link to the same file quickly?
Jordon Bedwell
jordon at envygeeks.com
Sat Apr 23 16:52:40 UTC 2011
On 4/23/2011 7:40 AM, Peng Yu wrote:
> I'm looking for a fast way to get all the hard links that point to the
> same file.
>
> file="/blah/blah"
> find $(stat -c %m "$file") -inum $(stat -c %i "$file")
>
> Currently, I have a slow way. It traverses the mount point that the
> file is on and look for all the files that have the same inum. But
> traversing the whole file system is a slow operation. Does the linux
> OS keep a table somewhere all the paths pointing to any file or the
> linux OS only keep the number of the paths that point to a given inum
> but does not keep the actual paths?
#!/bin/sh
folder='/path/to/folder'
file='/path/to/folder/file_being_hardlinked'
find ${folder} -inum $(stat -c %i ${file}) |sed -n '$!p'
---
Demo: (Demo#1 is the file, the rest are being hardlinked to Demo#1)
---
user at ubuntu:/path/to/folder# ls -l
total 24
-rw-r--r-- 5 user user 7 2011-04-23 09:12 Demo#1
-rw-r--r-- 5 user user 7 2011-04-23 09:12 Demo#2
-rw-r--r-- 5 user user 7 2011-04-23 09:12 Demo#3
-rw-r--r-- 5 user user 7 2011-04-23 09:12 Demo#4
-rw-r--r-- 5 user user 7 2011-04-23 09:12 Demo#5
-rw-r--r-- 1 user user 169 2011-04-23 09:38 hardlinks.sh
---
user at ubuntu:/path/to/folder# sh hardlinks.sh
/path/to/folder/Demo#4
/path/to/folder/Demo#5
/path/to/folder/Demo#2
/path/to/folder/Demo#3
---
To delete hardlinks leaving the primary file intact do:
#!/bin/sh
folder='/path/to/folder'
file='/path/to/folder/file_being_hardlinked'
find ${folder} -inum $(stat -c %i ${file}) |sed -n '$!p' |xargs rm
More information about the ubuntu-users
mailing list