递归查找多个文件,并在Linux重命名 [英] Finding multiple files recursively and renaming in linux

查看:307
本文介绍了递归查找多个文件,并在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.

谷歌建议我使用改名命令。所以,我执行命令重命名_dbg.txt .TXT * DBG * 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

执行后,改名命令,

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

它不这样做递归,如何使这个命令所有子目录中的重命名文件。像 XX YY 我将有这么多的子目录哪个名称是未predictable。也是我的 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.

推荐答案

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

You can use find to find all matching files recursively:

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


编辑:什么'{} \\

-exec 参数使得找到执行改名对于找到的每个匹配的文件。 '{} 将与文件的路径名替换。最后一个道理, \\; 仅在那里庆祝EXEC前pression年底

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆