重命名 unix shell 脚本中的所有文件名
Rename all file names in unix shell script
我正在尝试重命名目录中的所有文件。例如,这些文件名:
BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180112000002.dat_R52_1
BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180110000000.dat_R52_2
应重命名为:
BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180110111111.dat
BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180111222222.dat
以R51_1结尾的文件应该有一个时间戳,所有以R51_2结尾的文件都有另一个时间戳。这是代码:
#!/bin/ksh
x=$(date +%Y%m%d%H%M%S)
ls -lrt *BACMAIN* | cut -f9,10 -d'_' >tmp.txt
while read LINE; do
x=$(date +%Y%m%d%H%M%S)
filearray=`ls -1t *"$LINE"`
echo "${filearray[@]}"
for file in "${filearray[@]}"
do
mv "$file" "${file%_$LINE}"_$x.dat;
done
x=x++;
done < tmp.txt
...但出现如下错误:
BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180112000002.dat_R52_1
BPP_xxx_xxx-ASTEVSRC_11_xxx_1_1_20180112000002.dat_R52_1
BPP_xxx_xxx-ASTMAIN_11_xxx_1_1_20180112000002.dat_R52_1
BPP_xxx_xxx-BACBILLDET_12_xxx_1_1_20180112000002.dat_R52_1
BPP_xxx_xxx-BACMAIN_12_xxx_1_1_20180112000002.dat_R52_1
BPP_xxx_xxx-BACPAYDET_12_xxx_1_1_20180112000002.dat_R52_1
mv: accessing `BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180112000002.dat_R52_1\nBPP_xxx_xxx-ASTEVSRC_11_xxx_1_1_20180112000002.dat_R52_1\nBPP_xxx_xxx-ASTMAIN_11_xxx_1_1_20180112000002.dat_R52_1\nBPP_xxx_xxx-BACBILLDET_12_xxx_1_1_20180112000002.dat_R52_1\nBPP_xxx_xxx-BACMAIN_12_xxx_1_1_20180112000002.dat_R52_1\nBPP_xxx_xxx-BACPAYDET_12_xxx_1_1_20180112000002.dat_20180112151217.dat': File name too long
BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180110000000.dat_R52_2
BPP_xxx_xxx-ASTEVSRC_11_xxx_1_1_20180110000000.dat_R52_2
BPP_xxx_xxx-ASTMAIN_11_xxx_1_1_20180110000000.dat_R52_2
BPP_xxx_xxx-BACBILLDET_12_xxx_1_1_20180110000000.dat_R52_2
BPP_xxx_xxx-BACMAIN_12_xxx_1_1_20180110000000.dat_R52_2
BPP_xxx_xxx-BACPAYDET_12_xxx_1_1_20180110000000.dat_R52_2
mv: accessing `BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180110000000.dat_R52_2\nBPP_xxx_xxx-ASTEVSRC_11_xxx_1_1_20180110000000.dat_R52_2\nBPP_xxx_xxx-ASTMAIN_11_xxx_1_1_20180110000000.dat_R52_2\nBPP_xxx_xxx-BACBILLDET_12_xxx_1_1_20180110000000.dat_R52_2\nBPP_xxx_xxx-BACMAIN_12_xxx_1_1_20180110000000.dat_R52_2\nBPP_xxx_xxx-BACPAYDET_12_xxx_1_1_20180110000000.dat_20180112151217.dat': File name too long
您分配 filearray
一个常规字符串,但随后您将其用作数组。
你必须告诉shell你想要一个数组
typeset -a filearray
或
filearray=( $(ls -1t *[12]) )
为了调试,您可以在 mv
前面放一个 echo
,如果您得到了想要的结果,请将其删除。 或者定义debug=echo
,把$debug
放在关键命令的前面。完成后,定义 debug=
如需更多改进,请将代码放入 https://www.shellcheck.net/,并注意第 12 行。
我正在尝试重命名目录中的所有文件。例如,这些文件名:
BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180112000002.dat_R52_1
BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180110000000.dat_R52_2
应重命名为:
BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180110111111.dat
BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180111222222.dat
以R51_1结尾的文件应该有一个时间戳,所有以R51_2结尾的文件都有另一个时间戳。这是代码:
#!/bin/ksh
x=$(date +%Y%m%d%H%M%S)
ls -lrt *BACMAIN* | cut -f9,10 -d'_' >tmp.txt
while read LINE; do
x=$(date +%Y%m%d%H%M%S)
filearray=`ls -1t *"$LINE"`
echo "${filearray[@]}"
for file in "${filearray[@]}"
do
mv "$file" "${file%_$LINE}"_$x.dat;
done
x=x++;
done < tmp.txt
...但出现如下错误:
BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180112000002.dat_R52_1
BPP_xxx_xxx-ASTEVSRC_11_xxx_1_1_20180112000002.dat_R52_1
BPP_xxx_xxx-ASTMAIN_11_xxx_1_1_20180112000002.dat_R52_1
BPP_xxx_xxx-BACBILLDET_12_xxx_1_1_20180112000002.dat_R52_1
BPP_xxx_xxx-BACMAIN_12_xxx_1_1_20180112000002.dat_R52_1
BPP_xxx_xxx-BACPAYDET_12_xxx_1_1_20180112000002.dat_R52_1
mv: accessing `BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180112000002.dat_R52_1\nBPP_xxx_xxx-ASTEVSRC_11_xxx_1_1_20180112000002.dat_R52_1\nBPP_xxx_xxx-ASTMAIN_11_xxx_1_1_20180112000002.dat_R52_1\nBPP_xxx_xxx-BACBILLDET_12_xxx_1_1_20180112000002.dat_R52_1\nBPP_xxx_xxx-BACMAIN_12_xxx_1_1_20180112000002.dat_R52_1\nBPP_xxx_xxx-BACPAYDET_12_xxx_1_1_20180112000002.dat_20180112151217.dat': File name too long
BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180110000000.dat_R52_2
BPP_xxx_xxx-ASTEVSRC_11_xxx_1_1_20180110000000.dat_R52_2
BPP_xxx_xxx-ASTMAIN_11_xxx_1_1_20180110000000.dat_R52_2
BPP_xxx_xxx-BACBILLDET_12_xxx_1_1_20180110000000.dat_R52_2
BPP_xxx_xxx-BACMAIN_12_xxx_1_1_20180110000000.dat_R52_2
BPP_xxx_xxx-BACPAYDET_12_xxx_1_1_20180110000000.dat_R52_2
mv: accessing `BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180110000000.dat_R52_2\nBPP_xxx_xxx-ASTEVSRC_11_xxx_1_1_20180110000000.dat_R52_2\nBPP_xxx_xxx-ASTMAIN_11_xxx_1_1_20180110000000.dat_R52_2\nBPP_xxx_xxx-BACBILLDET_12_xxx_1_1_20180110000000.dat_R52_2\nBPP_xxx_xxx-BACMAIN_12_xxx_1_1_20180110000000.dat_R52_2\nBPP_xxx_xxx-BACPAYDET_12_xxx_1_1_20180110000000.dat_20180112151217.dat': File name too long
您分配 filearray
一个常规字符串,但随后您将其用作数组。
你必须告诉shell你想要一个数组
typeset -a filearray
或
filearray=( $(ls -1t *[12]) )
为了调试,您可以在 mv
前面放一个 echo
,如果您得到了想要的结果,请将其删除。 或者定义debug=echo
,把$debug
放在关键命令的前面。完成后,定义 debug=
如需更多改进,请将代码放入 https://www.shellcheck.net/,并注意第 12 行。