找到具有相同索引节点的所有文件的最快方法是什么?
我唯一知道的是:
find /home -xdev -samefile file1
但是它真的很慢。我想找到一个像locate
这样的工具。 当你有很多文件时,真正的问题就出现了,我认为操作是 O(n) 。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有从
inode
到名称的映射。唯一的方法是遍历整个文件系统,正如您所指出的,这是 O(文件数)。 (实际上,我认为是θ(文件数))。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)).我知道这是一个老问题,但是许多版本的 find 都有一个 inum 选项可以轻松匹配已知的 inode 编号。您可以使用以下命令来执行此操作:
如果允许这样做,这仍然会运行所有文件,但是一旦获得匹配项,您始终可以手动停止它;我不确定
find
是否有一个选项在一次匹配后停止(也许使用-exec
语句?)这比将输出转储到文件要容易得多,排序等和其他方法,因此应在可用时使用。
I know this is an old question, but many versions of
find
have aninum
option to match a known inode number easily. You can do this with the following command: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.
这是一种方法:
find -printf "%i:\t%p
或类似的方法创建以 inode 为前缀的所有文件的列表,并输出到临时文件cut -f 1 | sort | uniq -d
限制重复项,并将其输出到第二个临时文件fgrep - f
将第二个文件作为字符串列表加载以搜索和搜索第一个临时文件(当我写这篇文章时,我将问题解释为查找具有重复 inode 的所有文件。当然,可以使用前半部分的输出作为一种索引,从索引节点到路径,很像locate的工作方式。)
在我自己的机器上,我经常使用这些类型的文件,并保持它们排序我也有一个文本索引器。然后可以应用二分搜索来快速查找具有公共前缀的所有行,这样的工具对于此类工作非常有用。
Here's a way:
find -printf "%i:\t%p
or similar to create a listing of all files prefixed by inode, and output to a temporary filecut -f 1 | sort | uniq -d
, and output that to a second temporary filefgrep -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.