我在Suse 10系统中有a_dbg.txt, b_dbg.txt ...个这样的文件.我想编写一个bash shell脚本,通过从这些文件中删除"_dbg"来重命名这些文件.

谷歌建议我使用rename命令.所以我在CURRENT_FOLDER上执行了rename _dbg.txt .txt *dbg*命令

我的实际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

它不是递归的,如何使这个命令重命名所有子目录中的文件.像XXYY,我将有这么多子目录的名称是不可预测的.我的CURRENT_FOLDER也会有一些其他的文件.

推荐答案

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

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

EDIT: '{}'\;是什么?

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

所有这些都在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相关问答推荐

与mmap和munmap相比,保留一个巨大内存文件(约400 MB)的mmap多次的性能成本

加载ELF64头文件为什么会导致分段错误?

有没有办法确定什么代码使 linux 共享对象inflating ?

Powershell Core,MXLinuxv21,运行 linux /usr/bin/x 命令导致对象命令运行时找不到文件

在 Ubuntu 中,如何设置 Tomcat 9 以使用 Java 17?

ENQCMD 指令的好处和微操作是什么?

Linux上Kvaser数据库编辑器的等效工具

如何使用 __attribute__((visibility("default")))?

ld-linux.so.2 和 linux-gate.so.1 是什么?

应用程序如何在运行时解析为不同版本的共享库?

如何将 bash 别名定义为多个命令的序列?

如何在 Linux 上取消关机?

如何对 /dev/random 或 /dev/urandom 进行 base64 编码?

在 Emacs 中匹配括号的命令是什么?

如何在字符串中查找子字符串(或如何 grep 变量)?

错误:ld.so:无法预加载对象 LD_PRELOAD:忽略

如何以编程方式禁用硬件预取?

是否有git sed或类似功能?

基于shell中正则表达式的 colored颜色 突出显示输出

如何在 sed 中指定非捕获组?