我需要将文件夹中少数文件格式下的所有文件重命名,以便最后_2.txt
与apac
,emea
,mds
相同将在所有文件中相同,但在_XXX_2.txt
需要将logs_date
添加到所有文件之前。
ABC_xyz_123_apac_2.txt
POR5_emea_2.txt
qw_1_0_122_mds_2.txt
到
logs_date_apac_2.txt
logs_date_emea_2.txt
logs_date_mds_2.txt
答案 0 :(得分:0)
答案 1 :(得分:0)
我不确定但也许这就是你想要的:
#!/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
答案 2 :(得分:0)
如果你不想或不能使用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并以下划线结尾的字符,然后将匹配替换为“logs_date _”。
从bash手册页复制:
?(pattern-list) Matches zero or one occurrence of the given patterns *(pattern-list) Matches zero or more occurrences of the given patterns +(pattern-list) Matches one or more occurrences of the given patterns @(pattern-list) Matches one of the given patterns !(pattern-list) Matches anything except one of the given patterns
答案 3 :(得分:0)
使用mmv
命令应该很容易。
mmv '*_*_2.txt' 'logs_date_#2_2.txt' *.txt
答案 4 :(得分:0)
您也可以使用重命名工具:
rename 's/.+(_[a-z]+_[0-9].)/logs_date$1/' files
这将为您提供所需的输出。