0

I have 2 folder structure like this:

SOURCE_FOLDER_W_GOOD_NAMES
├── A_some_name.png
├── B_another_different_name.png
├── C_just_another_too.png
└── D_this_one_stop_this_example.png

Another folder:

DESTINATION_FOLDER_W_INCREMENT_NAMES
├── icon_0.icns
├── icon_1.icns
├── icon_2.icns
└── icon_3.icns

So basically, SOURCE_FOLDER already contains the files which are sorted in alphabetical order. This sort order is already matched with DESTINATION_FOLDER (eg: A_some_name.png === icon_0.icns)

I'm using this loop combination to rename the files:

i=0
j=0
for img in SOURCE_FOLDER/*.png; do
  for i in {0..10000}; do
    let j++ || true;
    mv "SOURCE_FOLDER/icon_$i.icns" "DESTINATION_FOLDER/${img%.*}.icns"
  done
done

I executed this on the command line (the above is formatted, I'm a one-liner guy) in the root of the above folder.

THE_FATHER_FOLDER
├── SOURCE_FOLDER_W_GOOD_NAMES
└── DESTINATION_FOLDER_W_INCREMENT_NAMES

Unfortunately, the above loop is not working as I expected. I'm certain that I'm wrong but I don't know where.

Please correct me.

UPDATED

Solution for the above question, I solved it myself

counter=0
for img in SOURCE_FOLDER/*.png; do
  let counter++ || true;
  mv "SOURCE_FOLDER/icon_$counter.icns" "DESTINATION_FOLDER/${img%.*}.icns"
done

But I have another question, in this folder:

DESTINATION_FOLDER_W_INCREMENT_NAMES
├── icon_0.icns
├── icon_1.icns
├── icon_2.icns
└── icon_3.icns

I want to reindex it from 1 instead of 0, thus I'm using this:

counter=0
for index in {0..final_number}; do
  let counter++ || true;
  mv "icon_${index}.icns" "icon_${counter}.icns";
done

At the moment, my files are started from 0 and ended up by final_number - 1, so it will overwrite every single-file I have in this folder and I got only the first file (icon_0) which renamed to (icon_final_number) when it's done.

How could I resolve this?

3 Answers 3

3

You want to do the rename backwards:

counter=$((final_number + 1))
for index in {final_number..0}; do
  mv "icon_${index}.icns" "icon_${counter}.icns";
  let counter--;
done
1
  • 1
    Thank you very much. I'm certain that I'm wrong about algorithm. You save my day. Commented May 11, 2016 at 3:39
1

Your problems will be solved by the following code:

#!/bin/sh

i=0
for img in `ls SOURCE_FOLDER_W_GOOD_NAMES/*.png`; do
        mv $img DESTINATION_FOLDER_W_INCREMENT_NAMES/icon_$i.icns
        i=$((i+1));
done
1
  • Thank for your opt-in but I already resolved it in my way. Please check the question, I have a new one :) Commented May 10, 2016 at 7:12
0

Initialize your counter with the value 1 instead of 0, and increase it after a move is done. Then you won't need to run a secondary bash script just for renaming the files, you would address it in the original script.

Also, why when you increase your counter you add " || true" ? it has no meaning. Try this code

counter=1
for img in SOURCE_FOLDER/*.png; do
  mv "SOURCE_FOLDER/icon_$counter.icns" "DESTINATION_FOLDER/${img%.*}.icns"
  let counter++
done
9
  • You don't understand, the secondary bash script will be executed firstly. And I need to use it in another place with same logic. I already solved and posted solution of the first one, why you post the solution of first one too? Commented May 10, 2016 at 8:16
  • I misunderstood you. try this then counter=1; for index in {0..final_number}; do mv "icon_${index}.icns" "icon_${counter}.icns"; let counter++; done
    – Chen A.
    Commented May 10, 2016 at 8:35
  • This will not work also, please read the question carefully. It will overwrite everything in that folder and return you only one file after this loop done. Commented May 10, 2016 at 9:01
  • Please specify what you want to be returned. As it seems, this loop will rename all the files in the folder, but I don't see any return action or what you expect to be returned in your question
    – Chen A.
    Commented May 10, 2016 at 10:06
  • Yes it will rename all files but it will OVERWRITE EVERYFILE till the loop done. Finally, you will only get ONLY ONE file. And as I said, please read the question carefully. Commented May 10, 2016 at 10:27

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.