我的朋友告诉我,可以使用 -r 开关在目录和子目录中递归查找文件。请告诉我给定语句中的错误它不起作用
find / -type f -r -name "abc.txt"
Run Code Online (Sandbox Code Playgroud)
ter*_*don 11
它不起作用的原因是因为 find 没有-r选择。虽然对于许多程序来说,该-r标志确实意味着“递归”,但并非所有情况都是如此,对于find. 的工作find是搜索文件和目录,您通常不希望它是递归的。
您可以使用该man命令检查大多数程序的选项。例如,man find。由于 find 手册很大,您可能需要搜索它的-r选项:
$ man find | grep -w -- -r
Run Code Online (Sandbox Code Playgroud)
-- 只是告诉 grep 停止读取选项,没有它,-r 将作为选项传递给 grep。此外,您可以通过点击/然后写下您想要搜索的内容,然后输入在手册页中进行搜索。
该命令不返回任何内容,将其与搜索以下手册的命令进行比较cp:
$ man cp | grep -w -- -r
-R, -r, --recursive
Run Code Online (Sandbox Code Playgroud)
由于find始终是递归的,因此它确实具有相反的功能,该标志可让您选择它应该下降到多少个子目录:
-maxdepth levels
Descend at most levels (a non-negative integer) levels of direc?
tories below the command line arguments. -maxdepth 0
means only apply the tests and actions to the command line
arguments.
-mindepth levels
Do not apply any tests or actions at levels less than levels (a
non-negative integer). -mindepth 1 means process all files
except the command line arguments.
Run Code Online (Sandbox Code Playgroud)
因此,每当您对命令有疑问时,请阅读其man页面,因为您永远不知道特定选项可能会做什么。例如:
$ man sort | grep -w -- -r
-r, --reverse
$ man mount | grep -w -- -r,
-r, --read-only
$ man awk | grep -A 8 -w -- -r
-r
--re-interval
Enable the use of interval expressions in regular expression
matching (see Regular Expressions, below). Interval expressions
were not traditionally available in the AWK language. The POSIX
standard added them, to make awk and egrep consistent with each
other. They are enabled by default, but this option remains for
use with --traditional.
$ man sed | grep -w -- -r
-r, --regexp-extended
$ man xterm | grep -w -- -r
-r This option indicates that reverse video should be simulated by
swapping the foreground and background colors.
Run Code Online (Sandbox Code Playgroud)
你明白了。