查找多个文件并在Linux中重命名它们 [英] Find multiple files and rename them in Linux
问题描述
我在Suse 10
系统中有类似a_dbg.txt, b_dbg.txt ...
的文件.我想编写一个bash shell脚本,该脚本应该通过从文件中删除"_dbg"来重命名这些文件.
I am having files like a_dbg.txt, b_dbg.txt ...
in a Suse 10
system. I want to write a bash shell script which should rename these files by removing "_dbg" from them.
Google建议我使用rename
命令.所以我在CURRENT_FOLDER
Google suggested me to use rename
command. So I executed the command rename _dbg.txt .txt *dbg*
on the CURRENT_FOLDER
我的实际CURRENT_FOLDER
包含以下文件.
My actual CURRENT_FOLDER
contains the below files.
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
它不是递归执行的操作,如何使此命令重命名所有子目录中的文件.像XX
和YY
一样,我将拥有这么多子目录,这些子目录的名称不可预测.而且我的CURRENT_FOLDER
也将拥有一些其他文件.
Its not doing recursively, how to make this command to rename files in all subdirectories. Like XX
and YY
I will be having so many subdirectories which name is unpredictable. And also my CURRENT_FOLDER
will be having some other files also.
推荐答案
您可以使用find
递归查找所有匹配的文件:
You can use find
to find all matching files recursively:
$ find . -iname "*dbg*" -exec rename _dbg.txt .txt '{}' \;
编辑:'{}'
和\;
是什么?
what the '{}'
and \;
are?
-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.
这篇关于查找多个文件并在Linux中重命名它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!