在 Linux 中查找多个文件并重命名它们 [英] Find multiple files and rename them in Linux

查看:38
本文介绍了在 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

它不是递归的,如何让这个命令重命名所有子目录中的文件.像 XXYY 一样,我会有很多子目录的名字是不可预测的.而且我的 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 '{}' ;

<小时>

'{}'; 是什么?

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

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