gpt4 book ai didi

linux - 查找命令中的 '{}' 是什么意思?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:22:03 27 4
gpt4 key购买 nike

我想知道 {} 的真正作用以及它在此命令中的工作原理:

find .-type f ! -name '*.c' -exec rm {} +

最佳答案

来自 man find :

-exec command ;

Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of ';' is encountered. The string '{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a '\') or quoted to protect them from expansion by the shell.

-exec command {} +

This variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invocations of the command will be much less than the number of matched files. The command line is built in much the same way that xargs builds its command lines. Only one instance of `{}' is allowed within the command. The command is executed in the starting directory.

所以它所做的是使用 find 命令的结果作为 rm 执行的参数。

然后,完整的命令

find .-type f ! -name '*.c' -exec rm {} +

将在当前目录结构中查找文件。它将匹配名称以 .c 结尾的那些。对于所有结果,它将执行 rm 命令,因此所有不是 *.c 的文件都将被删除。

示例:

$ find . -type f
./two/three
./something
$ find . -type f -exec ls -ltr {} +
-rw-rw-r-- 1 me me 0 Sep 30 11:37 ./something
-rw-r--r-- 1 me me 0 Nov 1 17:50 ./two/three

关于linux - 查找命令中的 '{}' 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19950892/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com