Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 文件夹中文件的批量重命名_Bash - Fatal编程技术网

Bash 文件夹中文件的批量重命名

Bash 文件夹中文件的批量重命名,bash,Bash,我需要重命名文件夹中少数文件格式下的所有文件,以使last _2.txt在所有文件中都是相同的,apac、emea、mds在所有文件中都是相同的,但在_XXX _2.txt之前,需要将日志添加到所有文件中 ABC_xyz_123_apac_2.txt POR5_emea_2.txt qw_1_0_122_mds_2.txt 到 你必须使用bash吗 批量重命名实用程序是一个非常棒的工具,可以以直观的方式轻松重命名多个文件 我不确定,但也许这就是你想要的: #!/bin/bash for f

我需要重命名文件夹中少数文件格式下的所有文件,以使last _2.txt在所有文件中都是相同的,apac、emea、mds在所有文件中都是相同的,但在_XXX _2.txt之前,需要将日志添加到所有文件中

ABC_xyz_123_apac_2.txt 
POR5_emea_2.txt
qw_1_0_122_mds_2.txt


你必须使用bash吗

批量重命名实用程序是一个非常棒的工具,可以以直观的方式轻松重命名多个文件


我不确定,但也许这就是你想要的:

#!/bin/bash

for file in *_2.txt;do
    # remove echo to rename the files once you check it does what you expect
    echo mv -v "$file" "$(sed 's/.*\(_.*_2\.txt\)$/logs_date\1/' <<<"$file")"
done

如果您不想或不能使用sed,您也可以尝试这个,它甚至可以运行得更快。无论您使用什么解决方案,如果可能,请确保在备份之前进行备份

shopt +s extglob    # turn on the extglob shell option, which enables several extended pattern matching operators
set +H    # turn off ! style history substitution
for file in *_2.txt;do
    # remove echo to rename the files once you check it does what you expect
    echo mv -v "$file" "${file/?(*_)!(*apac*|*emea*|*mds*)_/logs_date_}"
done
${parameter/pattern/string}执行模式替换。首先(可选)匹配以下划线结尾的多个字符,然后匹配以下数量的不包含apac、emea或mds且以下划线结尾的字符,然后将匹配替换为日志\日期\日期

从bash手册页复制:

使用mmv命令应该很容易

mmv '*_*_2.txt' 'logs_date_#2_2.txt' *.txt

您还可以使用重命名工具:

rename 's/.+(_[a-z]+_[0-9].)/logs_date$1/' files

这将为您提供所需的输出

谢谢你的帮助。。我编辑了一个问题,同样的脚本会有帮助吗???@Nirmal你可以试试看它是否有帮助,除非你删除回音,否则它不会改变任何东西。你应该详细说明你尝试过的内容-如果没有,到底有什么困难Hi Ruby,你能解释一下它是如何工作的吗?谢谢你的帮助。我补充了一个解释。
mmv '*_*_2.txt' 'logs_date_#2_2.txt' *.txt
rename 's/.+(_[a-z]+_[0-9].)/logs_date$1/' files