What is the fastest way to find all the file with the same inode?

大兔子大兔子 提交于 2019-12-03 07:42:42

问题


The only way I know is:

find /home -xdev -samefile file1

But it's really slow. I would like to find a tool like locate. The real problems comes when you have a lot of file, I suppose the operation is O(n).


回答1:


Here's a way:

  • Use find -printf "%i:\t%p or similar to create a listing of all files prefixed by inode, and output to a temporary file
  • Extract the first field - the inode with ':' appended - and sort to bring duplicates together and then restrict to duplicates, using cut -f 1 | sort | uniq -d, and output that to a second temporary file
  • Use fgrep -f to load the second file as a list of strings to search and search the first temporary file.

(When I wrote this, I interpreted the question as finding all files which had duplicate inodes. Of course, one could use the output of the first half of this as a kind of index, from inode to path, much like how locate works.)

On my own machine, I use these kinds of files a lot, and keep them sorted. I also have a text indexer application which can then apply binary search to quickly find all lines that have a common prefix. Such a tool ends up being quite useful for jobs like this.




回答2:


There is no mapping from inode to name. The only way is to walk the entire filesystem, which as you pointed out is O(number of files). (Actually, I think it's θ(number of files)).




回答3:


I know this is an old question, but many versions of find have an inum option to match a known inode number easily. You can do this with the following command:

find . -inum 1234

This will still run through all files if allowed to do-so, but once you get a match you can always stop it manually; I'm not sure if find has an option to stop after a single match (perhaps with an -exec statement?)

This is much easier than dumping output to a file, sorting etc. and other methods, so should be used when available.




回答4:


What I'd typically do is: ls -i <file> to get the inode of that file, and then find /dir -type f -inum <inode value> -mount. (You want the -mount to avoid searching on different file systems, which is probably part of your performance issues.)

Other than that, I think that's about it.



来源:https://stackoverflow.com/questions/1340263/what-is-the-fastest-way-to-find-all-the-file-with-the-same-inode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!