我有很多这样的文件
BBG-06-0645-01B.txt
BFDG_06-0219-01T.txt
MFD-02-0047-011T.txt
BBTF_06-0649-01N.txt
BFGD_02-2486-016J.txt
我正在尝试用下划线替换所有连字符,例如以下
BBG_06_0645_01B.txt
BFDG_06_0219_01T.txt
MFD_02_0047_011T.txt
BBTF_06_0649_01N.txt
BFGD_02_2486_016J.txt
我尝试不使用sucecss使用以下内容
rename -nvs - _ *.txt
Using expression: sub { use feature ':5.18'; s/\Q${\"\-"}/_/ }
'BBG-06-0645-01B.txt' would be renamed to 'BBG_06-0645-01B.txt'
'BBTF_06-0649-01N.txt' would be renamed to 'BBTF_06_0649-01N.txt'
'BFDG_06-0219-01T.txt' would be renamed to 'BFDG_06_0219-01T.txt'
'BFGD_02-2486-016J.txt' would be renamed to 'BFGD_02_2486-016J.txt'
'MFD-02-0047-011T.txt' would be renamed to 'MFD_02-0047-011T.txt'
如果我也使用rename 's/\Q${\"\-"}/_/' *.txt
,则不会更改-全部更改为_
答案 0 :(得分:1)
在这里使用纯BASH替换。您能不能试一下。(仅适用于空转)
for file in *.txt
do
echo "mv $file ${file//-/_}"
done
仅当您对命令(mv
命令)感到满意之后,才会从上面的命令中删除echo
。这只会首先打印语句。
这是输出mv BBG-06-0645-01B.txt BBG_06_0645_01B.txt
一旦您对以上使用以下结果感到满意。
for file in *.txt
do
echo "File named $file is going to rename to ${file//-/_}"
mv "$file" "${file//-/_}"
done
如果要显示状态,则已成功重命名或不使用的文件:
for file in *.txt
do
echo "File named $file is going to rename to ${file//-/_}"
if mv "$file" "${file//-/_}"
then
echo "File named $file is successfully renamed to ${file//-/_}"
else
echo "Please check seems file $file did not rename."
fi
done
答案 1 :(得分:1)
您可以像这样使用()
:
rename
带有rename "s/-/_/g" *.txt
标志的空运行:
-n
用for循环替换bash :
rename -n "s/-/_/g" *.txt
以for file in *.txt; do mv "$file" "${file//-/_}" ; done
而非echo
进行空运行:
mv