重命名以数值结尾的文本文件

时间:2014-11-06 18:51:57

标签: linux bash unix awk sed

我有15个名为... cyclones_hrrr _( 1-16 )的文件,我需要将它们重命名为cyclones_hrrr _( 0-15 )。我使用mv命令编写了以下代码并且它可以工作但是,我需要更有效的东西,也许是某种循环:

mv ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_1 ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_0
mv ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_2 ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_1
mv ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_3 ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_2
mv ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_4 ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_3
mv ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_5 ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_4
mv ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_6 ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_5
mv ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_7 ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_6
mv ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_8 ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_7
mv ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_9 ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_8
mv ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_10 ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_9
mv ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_11 ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_10
mv ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_12 ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_11
mv ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_13 ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_12
mv ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_14 ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_13
mv ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_15 ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_14
mv ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_16 ${HOMEDIR}/hrrr/tmp/cyclones_hrrr_15

2 个答案:

答案 0 :(得分:2)

尝试:

p="${HOMEDIR}/hrrr/tmp/cyclones_hrrr_"
for ((i=1; i<=16; i++)); do mv "$p$i" "$p$((i-1))" ; done

答案 1 :(得分:1)

for i in {1..16}; do mv "cyclones_hrrr_$i" "cyclones_hrrr_$((i-1))" ; done

解释

  • for i in {1..16}; do

    这会启动一个循环。对于变量i的每个值,从1到16运行循环。

  • mv "cyclones_hrrr_$i" "cyclones_hrrr_$((i-1))"

    这会移动文件。如果文件不在当前目录中,请添加路径。

    shell允许简单的算术运算。表达式$((i-1))返回i-1

  • done

    这表示循环结束。