使用一个变量重命名多个文件

使用一个变量重命名多个文件

我正在尝试用一个变量重命名一个文件。

示例:2014.001.**.**.**.D890.E.mIP.PMST..BHE.D.2014.001.

我的脚本是:

#!/bin/bash

cd /home/pintolcv/Downloads

for jd in {001..366}
do

mv 2014.$jd.*.*.D890.E.m IP.PMST..BHE.D.2014.$jd 

done

但我总是看到这个错误:

mv: cannot stat `2014.001.*.*.D890.E.m': No such file or directory
mv: cannot stat `2014.002.*.*.D890.E.m': No such file or directory
mv: cannot stat `2014.003.*.*.D890.E.m': No such file or directory
mv: cannot stat `2014.004.*.*.D890.E.m': No such file or directory
mv: cannot stat `2014.005.*.*.D890.E.m': No such file or directory
mv: cannot stat `2014.006.*.*.D890.E.m': No such file or directory
mv: cannot stat `2014.007.*.*.D890.E.m': No such file or directory
mv: cannot stat `2014.008.*.*.D890.E.m': No such file or directory
..
..
.. and so on!


我怎样才能让它发挥作用?

答案1

固定脚本:

for file in /home/pintolcv/Downloads/2014*D890.E.m; do

filepath=${file%/*}
filebasename=${file##*/}

#echo "File equals: $file"
#echo "File path equals: $filepath"
#echo "File basename equals: $filebasename"

    if [[ -e "$file" ]]; then
        echo "File Found: $filebasename - ${filebasename:5:3}"
        mv "$file" "$filepath/IP.PMST.BHE..D.2014.${filebasename:5:3}"
    else
        echo "files do not exist"
    fi

done

相关内容