gpt4 book ai didi

bash - 如何删除 "find"命令的所有结果

转载 作者:行者123 更新时间:2023-12-04 12:18:46 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Why are the backslash and semicolon required with the find command's -exec option?

(2 个回答)


4年前关闭。




我有一个包含几个以 .pyc 结尾的文件的目录,我想一次性全部删除。我都试过了

find . -name '*pyc' | rm


find . -name '*pyc' -exec rm

但是,这些语句都不符合用法(在第一种情况下)或语法正确(在第二种情况下,我得到 find: -exec: no terminating ";" or "+" )。如何将结果“管道”到“删除”命令?

最佳答案

find . -name '*.pyc' -exec rm -- '{}' +

来自 man find :

  • -exec utility [argument ...] ; True if the program named utility returns a zero value as its exit status. Optional arguments may be passed to the utility. The expression must be terminated by a semicolon (;). If you invoke find from a shell you may need to quote the semicolon if the shell would otherwise treat it as a control operator. If the string {} appears anywhere in the utility name or the arguments it is replaced by the pathname of the current file. Utility will be executed from the directory from which find was executed. Utility and arguments are not subject to the further expansion of shell patterns and constructs.

  • -exec utility [argument ...] {} + Same as -exec, except that {} is replaced with as many pathnames as possible for each invocation of utility. This behaviour is similar to that of > xargs(1).


{}不需要在 bash 中引用,但这有助于与其他一些扩展 shell 兼容。

关于bash - 如何删除 "find"命令的所有结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45039127/

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