3

I've got a problem i'm facing for 6-7 hours and i can't overcome it , that's why i'm asking for your help.I need to create a bash script that accesses a folder and renames all the files from it with a new name followed by a number that represents alalphabetic hierachy and a new extension.

The parameters are in this order:
(The new name , the new extension , and the folder where i'm searching).
As an example : ./rename.sh work .txt Documents
Let's say that initially the document (Documents) contained :
a.txt
b.txt
c.txt
Therefore , afer running the script the document will containt the following: work01.txt(from a.txt)
work02.txt(from b.txt)
work03.txt(from c.txt)

The problem is that somehow i'm duplicating the files and i'm creating a big mess. Here is the script (rename.sh):

#!/bin/bash
if [ -z "$3" ]
   then
    echo "No argumets supplied"
   else
    mic="0"
    i="1"
    max="9"
    for file in `ls $3 | sort -V`; do
        if ( test $i -le 9)
          then
            cp $3/$file $3/$1$mic$i.$2
            rm -r $3/$file
          else
            if ( test $i -ge 9 )
              then          
                cp $3/$file $3/$1$i.$2
                rm -r $3/$file
                fi
          fi
        i=$((i+1))  
    done
    echo "$max"
fi
  • Not sure exactly what you're trying to do, but I'd suggest using the bash shell's builtin printf to construct the newname with suitable zero-padded format (rather than trying to hack together something based on different variables for different digits) e.g. printf -v newname 'work%02d.txt' $((i++)) – steeldriver Jan 9 '16 at 17:19
  • @steeldriver for single digit numbers a 0 is added before them ,that's all.For numbers with more than 2 digits there is nothing more to be done.If i wasnt't explicit enough you can ask me questions about things i've forgot to mention. – gigiman Jan 9 '16 at 17:24
  • Well, are your files really called a.txt, b.txt etc or is that just meant to illustrate an ordering? What do you want to happen if a file is missing e.g. if h.txt doesn't exist, should i.txt become work08.txt or still be renamed to work09.txt regardless? – steeldriver Jan 9 '16 at 17:31
  • @steeldriver i.txt should become work08.txt – gigiman Jan 9 '16 at 17:37
  • this is still unsolved.. – gigiman Jan 13 '16 at 11:29
3

If your files are named a.txt, b.txt and so on, then the default collation order should allow you to do a simple filename glob expansion, rather than needing to explicitly version-sort the files. So I'd suggest doing something more like

i=1

cd "$dir"
for old in *."$ext"; do 
  printf -v new '%s%02d.%s' "$newbase" $((i++)) "$ext"
  echo mv "$old" "$new"
done

after assigning newbase="$1", ext="$2", dir="$3" (with suitable existence checks of course). Play with it and remove the echo once the printed output looks correct.

  • Forr some reason they are not modified accordingly. a.txt won't become thenewname01.x etc. I don't know the reason. I think i must read them sorted..but i don't know how to do that. The files in the folder are something like image1.pdf image2.pdf image3.pdf etc. – gigiman Jan 9 '16 at 20:56
  • The files from the folder might have a diffrent extension than the ones that are going to be created. So i don't think that "for" will work. – gigiman Jan 9 '16 at 21:25
  • That's why I asked if your files were really called a.txt, b.txt and so on – steeldriver Jan 13 '16 at 12:38

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.