NB! I have already started a thread on this subject on ComputerHope (with the thread title "Recursive File Renaming & Logical Sorting"). However, on ComputerHope they are perhaps more specialized in Windows Command Prompt, and not that much in shell scripts for Bash (I guess).
I have a bulk renaming problem that I have to tackle in some way with the help of a shell script for Bash. I should write a shell script to recursively rename all the image files in a folder and its subfolders to "001", "002", "003" and so forth. The aim is to preserve all the file extensions (and my image files have many different extensions: jpg, jpeg, png, bmp and so on). All the folder names should also remain unchanged. Here's a short example showing what I mean:
rbots-ctf\cf-1024.jpg ----> 001.jpg
rbots-ctf\cf-1280.jpg ----> 002.jpg
rbots-ctf\cf-1440.jpg ----> 003.jpg
rbots-ctf\cf-1600.jpg ----> 004.jpg
chrome\backblue.jpg ----> 001.jpg
chrome\fade.jpg ----> 002.jpg
chrome\skulltag.bmp ----> 003.bmp
dmoz\newtip1.png ----> 001.png
dmoz\newtip2.png ----> 002.png
dmoz\newtip10.png ----> 003.png
dmoz\newtip11.png ----> 004.png
dmoz\pong.gif ----> 005.gif
dmoz\pong.png ----> 006.png
...and so on!
As you can see, once the script is applying new filenames to the files that it can find, it starts from "001" in each folder.
Thus far, I have tried to put together a batch for Windows Command Prompt and a shell script for Bash to accomplish this. The problem with Command Prompt is that there does not seem to be a way of displaying files in "human" numerical order unless the files were first renamed with zero-padded filenames (which would be very time-consuming because I have hundreds of folders with hundreds of subfolders full of images). In other words, there is no "logical sorting" or "natural sorting" as it may sometimes be referred to as. Once you type "dir" in Windows Command Prompt, you'll get a result like this:
1.jpg
10.jpg
11.jpg
12.jpg
2.jpg
3.jpg
4.jpg
5.jpg
6.jpg
7.jpg
8.jpg
9.jpg
...whereas "natural sorting" would give you a result like this:
1.jpg
2.jpg
3.jpg
4.jpg
5.jpg
6.jpg
7.jpg
8.jpg
9.jpg
10.jpg
11.jpg
12.jpg
As you can notice, the problem is that 10-12 are displayed before 2-9 in Windows Command Prompt. Thus, if the files are renamed with a batch for Windows Command Prompt, the lack of any "natural sorting" will inevitably affect the renaming. In Bash, however, this problem can be bypassed by piping your preferred directory listing command through "sort -h". For example, "ls|sort -h" displays all the files in "human" numerical order.
The problem is that I'm a complete beginner with Bash, and I did my utmost to put together a shell script but everything was to no avail. I also ran many different ready-made scripts (with some minor changes) without any success. For example, I found the shell script below on Stack Overflow but it doesn't do quite what I want it to do even though I did my best to edit it:
find -iname '*.jpg' | sort -h # works only for .jpg files ATM...
gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.jpg\n", $0, a++ }' | # build mv command
bash # run that command
Do you have any suggestions on how to remedy this problem with Bash so that I could rename my image files in the way explained above? I really wanted to do this by myself but I don't have the skills so any help you can provide would be most welcome!
P.S. The images are on my Windows PC but I'm using Cygwin (a Unix-like environment and command-line interface for Windows) to run all shell scripts for Bash.