Ubuntu – Script to rename files

bashrenamescripts

I appreciate any help in advance. I have written a script to rename all .BMP files, and it seems to work, but I get the following error/message for each file:

IM2019.BMP
mv: cannot stat `IM2000.BMP': No such file or directory
mv: cannot stat `IM2001.BMP': No such file or directory
mv: cannot stat `IM2002.BMP': No such file or directory
mv: cannot stat `IM2003.BMP': No such file or directory
mv: cannot stat `IM2004.BMP': No such file or directory
mv: cannot stat `IM2005.BMP': No such file or directory
mv: cannot stat `IM2006.BMP': No such file or directory
mv: cannot stat `IM2007.BMP': No such file or directory
mv: cannot stat `IM2008.BMP': No such file or directory
mv: cannot stat `IM2009.BMP': No such file or directory
mv: cannot stat `IM2010.BMP': No such file or directory
mv: cannot stat `IM2011.BMP': No such file or directory
mv: cannot stat `IM2012.BMP': No such file or directory
mv: cannot stat `IM2013.BMP': No such file or directory
mv: cannot stat `IM2014.BMP': No such file or directory
mv: cannot stat `IM2015.BMP': No such file or directory
mv: cannot stat `IM2016.BMP': No such file or directory
mv: cannot stat `IM2017.BMP': No such file or directory
mv: cannot stat `IM2018.BMP': No such file or directory
mv: cannot stat `IM2019.BMP': No such file or directory

I have the following files in the directory:

hristo@HassanGrad27:~/Desktop/Test$ ls
IM0000.BMP  IM0011.BMP  IM1002.BMP  IM1013.BMP  IM2004.BMP  IM2015.BMP
IM0001.BMP  IM0012.BMP  IM1003.BMP  IM1014.BMP  IM2005.BMP  IM2016.BMP
IM0002.BMP  IM0013.BMP  IM1004.BMP  IM1015.BMP  IM2006.BMP  IM2017.BMP
IM0003.BMP  IM0014.BMP  IM1005.BMP  IM1016.BMP  IM2007.BMP  IM2018.BMP
IM0004.BMP  IM0015.BMP  IM1006.BMP  IM1017.BMP  IM2008.BMP  IM2019.BMP
IM0005.BMP  IM0016.BMP  IM1007.BMP  IM1018.BMP  IM2009.BMP  rename
IM0006.BMP  IM0017.BMP  IM1008.BMP  IM1019.BMP  IM2010.BMP  rename~
IM0007.BMP  IM0018.BMP  IM1009.BMP  IM2000.BMP  IM2011.BMP  stringtest
IM0008.BMP  IM0019.BMP  IM1010.BMP  IM2001.BMP  IM2012.BMP  stringtest~
IM0009.BMP  IM1000.BMP  IM1011.BMP  IM2002.BMP  IM2013.BMP
IM0010.BMP  IM1001.BMP  IM1012.BMP  IM2003.BMP  IM2014.BMP

and I can get my files to be renamed as such:

hristo@HassanGrad27:~/Desktop/Test$ ls
cam1-0.BMP   cam1-3.BMP   cam2-15.BMP  cam2-9.BMP   cam3-2.BMP
cam1-10.BMP  cam1-4.BMP   cam2-16.BMP  cam3-0.BMP   cam3-3.BMP
cam1-11.BMP  cam1-5.BMP   cam2-17.BMP  cam3-10.BMP  cam3-4.BMP
cam1-12.BMP  cam1-6.BMP   cam2-18.BMP  cam3-11.BMP  cam3-5.BMP
cam1-13.BMP  cam1-7.BMP   cam2-19.BMP  cam3-12.BMP  cam3-6.BMP
cam1-14.BMP  cam1-8.BMP   cam2-1.BMP   cam3-13.BMP  cam3-7.BMP
cam1-15.BMP  cam1-9.BMP   cam2-2.BMP   cam3-14.BMP  cam3-8.BMP
cam1-16.BMP  cam2-0.BMP   cam2-3.BMP   cam3-15.BMP  cam3-9.BMP
cam1-17.BMP  cam2-10.BMP  cam2-4.BMP   cam3-16.BMP  rename
cam1-18.BMP  cam2-11.BMP  cam2-5.BMP   cam3-17.BMP  rename~
cam1-19.BMP  cam2-12.BMP  cam2-6.BMP   cam3-18.BMP  stringtest
cam1-1.BMP   cam2-13.BMP  cam2-7.BMP   cam3-19.BMP  stringtest~
cam1-2.BMP   cam2-14.BMP  cam2-8.BMP   cam3-1.BMP

This is the script:

#!/bin/bash

# Specify directory
for i in $(ls *.BMP) 
    do str="$i"
    echo $str
    charim=${str:2:1}
    #echo $charim

    if [ "$charim" -eq 0 ] && [ "$charim" -ne 1 ] && [ "$charim" -ne 2 ];       then
        #echo "1"
        for j in {0..9}
        do mv IM000$j.BMP cam1-$j.BMP
        done
        for j in {10..19}
        do mv IM00$j.BMP cam1-$j.BMP
        done
    fi 

    if [ "$charim" -ne 0 ] && [ "$charim" -eq 1 ] && [ "$charim" -ne 2 ];       then
    #echo "2"
        for j in {0..9}
        do mv IM100$j.BMP cam2-$j.BMP
        done
        for j in {10..19}
        do mv IM10$j.BMP cam2-$j.BMP
        done
    fi

    if [ "$charim" -ne 0 ] && [ "$charim" -ne 1 ] && [ "$charim" -eq 2 ];       then
    #echo "3"
        for j in {0..9}
        do mv IM200$j.BMP cam3-$j.BMP
        done
        for j in {10..19}
        do mv IM20$j.BMP cam3-$j.BMP
        done
    fi

done

What causes the error/warning?

Thanks a lot

Best Answer

for f in IM*.BMP; do
    n1=${f:2:1}
    n2=${f:3:3}
    new="cam$(( n1 + 1 ))-$(( 10#$n2 )).BMP"
    echo mv "$f" "$new"
done

$(( ... )) is an arithmetic expansion. The 10#$n2 forces the contents of the n2 variable to be handled as a base-10 number, and the leading zeroes are dropped: you cannot use $(( n2 + 0 )) because you'd get an "invalid octal" error for n2 == 008. The $ is optional inside an arithmetic expression, unless you use the base# notation

Related Question