且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Linux 中查找多个文件并重命名它们

更新时间:2023-11-22 22:14:28

您可以使用 find 递归查找所有匹配的文件:

You can use find to find all matching files recursively:

$ find . -iname "*dbg*" -exec rename _dbg.txt .txt '{}' ;

'{}'; 是什么?

-exec 参数使 find 对找到的每个匹配文件执行 rename.'{}' 将替换为文件的路径名.最后一个标记 ; 仅用于标记 exec 表达式的结尾.

The -exec argument makes find execute rename for every matching file found. '{}' will be replaced with the path name of the file. The last token, ; is there only to mark the end of the exec expression.

查找手册页中详细描述的所有内容:

All that is described nicely in the man page for 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 argu-
         ments 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.