1

I am trying to rename one file with one variable.

Example: 2014.001.**.**.**.D890.E.m to IP.PMST..BHE.D.2014.001.

My script is:

#!/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

But I always see this error:

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!


How can I get this working?

4
  • Please provide an example filename (without wildcards) so we can see what you are trying to match. – zackse Feb 20 '15 at 15:30
  • 1
    there are 3 level of * in the first line, and 2 * in the shell. – Archemar Feb 20 '15 at 15:30
  • 1
    now I come to think of it mv 2014.$jd.*.D890.E.m IP.PMST..BHE.D.2014.$jd (with one *) sould be enough. – Archemar Feb 20 '15 at 15:32
  • Just quote it. If you have a file named 2014.001.**.**.**.D890.E.m then you should put quotes around its name when attempting to rename it. Also, stop naming your files like that. – mikeserv Feb 20 '15 at 15:59
1

Fixed script:

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
3
  • Hi DevNull, Thanks for your time to help me. Something missing: The output on terminal is: File Found: /home/pintolcv/Downloads/2014.046.00.00.00.D890.E.m - /pi mv: cannot move /home/pintolcv/Downloads/2014.046.00.00.00.D890.E.m' to IP.PMST.BHE..D.2015./pi': No such file or directory files do not exist But the file that i want to rename is on /home/pintolcv/Downloads/2014.046.00.00.00.D890.E.m Thanks, LP – user3683346 Feb 23 '15 at 11:43
  • Sorry DevNull, It's not my personal convention. I never would choose this parameters. pintolcv@lapsis122:~/Downloads$ ls -l 2014.046.00.00.00.D890.E.m -rw-r--r-- 1 pintolcv pintolcv 7548928 Feb 23 11:28 2014.046.00.00.00.D890.E.m It's a file, not a directory. Yes two dots. It's not a typo error. It was like my first question. Thank you in advance for your patience. LP – user3683346 Feb 23 '15 at 13:59
  • @user3683346 I am sorry. When I wrote that script it was late and I completely overlooked important things like paths during the moves since I tested in local directories. Try the above script. That should work. I also included the echos (although they are commented out) for debugging if anything still fails. – devnull Feb 23 '15 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.