bash 查找具有相同 inode 的所有文件的最快方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1340263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the fastest way to find all the file with the same inode?
提问by Breezeight
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).
但它真的很慢。我想找到一个像locate. 当你有很多文件时,真正的问题就来了,我想这个操作是 O(n)。
采纳答案by Barry Kelly
Here's a way:
这里有一个方法:
- Use
find -printf "%i:\t%por 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 -fto load the second file as a list of strings to search and search the first temporary file.
- 使用
find -printf "%i:\t%p或类似的方法创建以 inode 为前缀的所有文件的列表,并输出到临时文件 - 提取第一个字段 - 附加了 ':' 的 inode - 并排序以将重复项放在一起,然后限制为重复项,使用
cut -f 1 | sort | uniq -d,并将其输出到第二个临时文件 - 使用
fgrep -f加载第二个文件作为一个字符串列表搜索和搜索第一个临时文件。
(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.)
(当我写这篇文章时,我将这个问题解释为查找所有具有重复 inode 的文件。当然,可以将前半部分的输出用作一种索引,从 inode 到路径,就像 locate 的工作方式一样。 )
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.
在我自己的机器上,我经常使用这些类型的文件,并将它们分类。我还有一个文本索引器应用程序,它可以应用二进制搜索来快速找到所有具有公共前缀的行。这样的工具最终对这样的工作非常有用。
回答by J?rg W Mittag
There is no mapping from inodeto 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)).
没有从inode到名称的映射。唯一的方法是遍历整个文件系统,正如您所指出的那样是 O(文件数)。(实际上,我认为是θ(文件数))。
回答by Haravikk
I know this is an old question, but many versions of findhave an inumoption to match a known inode number easily. You can do this with the following command:
我知道这是一个老问题,但许多版本find都有一个inum选项可以轻松匹配已知的 inode 编号。您可以使用以下命令执行此操作:
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 findhas an option to stop after a single match (perhaps with an -execstatement?)
如果允许,这仍然会遍历所有文件,但是一旦您获得匹配项,您始终可以手动停止它;我不确定是否find可以选择在单场比赛后停止(也许有-exec声明?)
This is much easier than dumping output to a file, sorting etc. and other methods, so should be used when available.
这比将输出转储到文件、排序等和其他方法要容易得多,因此应在可用时使用。
回答by John Feminella
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 -mountto avoid searching on different file systems, which is probably part of your performance issues.)
我通常会做的是:ls -i <file>获取该文件的 inode,然后find /dir -type f -inum <inode value> -mount. (您希望-mount避免在不同的文件系统上进行搜索,这可能是性能问题的一部分。)
Other than that, I think that's about it.
除此之外,我认为仅此而已。

