小编典典

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

linux

我有类似的文件a_dbg.txt, b_dbg.txt ...Suse 10系统中。我想编写一个bash
shell脚本,该脚本应该通过从文件中删除“ _dbg”来重命名这些文件。

Google建议我使用rename命令。所以我rename _dbg.txt .txt *dbg*CURRENT_FOLDER

我的实际CURRENT_FOLDER文件包含以下文件。

CURRENT_FOLDER/a_dbg.txt
CURRENT_FOLDER/b_dbg.txt
CURRENT_FOLDER/XX/c_dbg.txt
CURRENT_FOLDER/YY/d_dbg.txt

执行rename命令后,

CURRENT_FOLDER/a.txt
CURRENT_FOLDER/b.txt
CURRENT_FOLDER/XX/c_dbg.txt
CURRENT_FOLDER/YY/d_dbg.txt

它不是递归执行的操作,如何使此命令重命名所有子目录中的文件。Like
XXYY我将有很多子目录,这些子目录的名称不可预测。而且我CURRENT_FOLDER还将拥有一些其他文件。


阅读 747

收藏
2020-06-02

共1个答案

小编典典

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

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

编辑: 什么'{}'\;是谁?

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

手册页中对找到的所有内容进行了很好的描述:

 -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.
2020-06-02