我有几个文件需要使用linux shell脚本重命名。我需要插入文字" ID"在文件名的第二个字符旁边:
HP0001.txt
HP0002.txt
HP0003.txt
脚本应该能够将其重命名为
HPID0001.txt
HPID0002.txt
HPID0003.txt
答案 0 :(得分:1)
如果它只是“几个文件”,那么手动重命名可能最简单。
否则,这是一个你可以适应的简单脚本:
for f in HP*.txt; do
f2=`echo $f|sed -e 's/HP/HPID/'`
echo mv $f $f2
done
示例输出:
ls HP*
HP001.txt HP002.txt HP003.txt
bash ./tmp.sh
mv HP001.txt HPID001.txt
mv HP002.txt HPID002.txt
mv HP003.txt HPID003.txt
答案 1 :(得分:0)
HP0001.txt
HP0002.txt
HP0003.txt
main
cat main.sh
for file in *
do
new_name=$(echo $file|sed 's/HP/&ID/')
mv $file $new_name 2>/dev/null
done
ls -1
HPID0001.txt
HPID0002.txt
HPID0003.txt
main