find命令如何搜索文件 [英] How does the find command search for files
问题描述
从另一个线程中查询移动文件已经老化了x分钟,这个问题出现了:
Quarrying out from another thread Move file that has aged x minutes, this question came up:
通常在Linux中找到的find
命令如何在当前目录中搜索文件?
How does the find
command found typically in Linux search for files in the current directory?
考虑一个包含大量文件的目录,然后:
Consider a directory that contains a fairly large amount of files, then:
首先find MY_FILE.txt
立即返回,然后find . -name MY_FILE.txt
需要更长的时间.
Firstly find MY_FILE.txt
returns immediately and secondly find . -name MY_FILE.txt
takes much longer.
我使用strace -c
来查看这两种情况的发生,并且我了解到第二个命令调用了目录扫描,这解释了为什么它比较慢.
I used strace -c
to see what happens for both and I learned that the second command invokes a directory scan, which explains why it's slower.
因此,必须对第一个命令进行优化.谁能指出我合适的资源或快速解释如何实现?
So, the first command must be optimized. Can anybody point me to the appropriate resource or provide a quick explanation how this might be implemented?
推荐答案
find的语法为find <paths> <expression>
,其中 paths 是开始搜索的文件和目录的列表.从这些位置开始查找,然后递归(如果它们是目录).
The syntax for find is find <paths> <expression>
, where paths is a list of files and directories to start the search from. find starts from those locations and then recurses (if they're directories).
编写find . -name MY_FILE.txt
时,它将在./
目录下执行递归搜索.但是,如果您编写find MY_FILE.txt
,则是告诉它在./MY_FILE.txt
处开始搜索,所以它会这样做:
When you write find . -name MY_FILE.txt
it performs a recursive search under the ./
directory. But if you write find MY_FILE.txt
then you're telling it to start the search at ./MY_FILE.txt
, and so it does:
$ strace -e file find MY_FILE.txt
...
newfstatat(AT_FDCWD, "MY_FILE.txt", 0x556688ecdc68, AT_SYMLINK_NOFOLLOW) = -1 ENOENT (No such file or directory)
...
(No such file or directory)
: No such file or directory
+++ exited with 1 +++
由于该路径不存在,因此只需一个系统调用即可确定不存在此类文件.它调用newfstat()
,得到一个No such file or directory
错误,就是这样.
Since the path doesn't exist, it only takes a single system call to determine that there's no such file. It calls newfstat()
, gets a No such file or directory
error, and that's that.
换句话说,find MY_FILE.txt
不等同于find . -name MY_FILE.txt
.哎呀,因为您不要求它进行搜索,我什至认为它没有用.您只是要求它告诉您MY_FILE.txt
在当前目录中是否存在.但是您只需调用ls MY_FILE.txt
就能找到答案.
In other words, find MY_FILE.txt
isn't equivalent to find . -name MY_FILE.txt
. Heck, I wouldn't even call it useful because you're not asking it to search. You're just asking it to tell you if MY_FILE.txt
exists in the current directory or not. But you could find that out by simply calling ls MY_FILE.txt
.
区别在于:
[~]$ cd /usr
[/usr]$ find . -name sha384sum
./bin/sha384sum
[/usr]$ find sha384sum
find: ‘sha384sum’: No such file or directory
第一个执行递归搜索并找到/usr/bin/sha384sum
.第二个不递归并立即失败,因为/usr/sha384sum
不存在.它看起来没有更深.这是在一纳秒内完成的.
The first one performs a recursive search and finds /usr/bin/sha384sum
. The second one doesn't recurse and immediately fails bcause /usr/sha384sum
doesn't exist. It doesn't look any deeper. It's done in a nanosecond.
这篇关于find命令如何搜索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!