Sorting Numbers For Linux Scripting

How to sort through files that are listed 1-20, like File1, File10, File20, and then I want to do rename them so that they are padded to 4 digits. How would I do that, considering normally files would go File1, then File10?

Ans:

I think this should get the whole thing done.  Well, at least, on bash it works! 

Basical logic is:

get filenames;
strip string from filename;
strip number from filename;
check lengt of number part of filename;
add as much 0es as needed to number part;
rename file;

#!/bin/bash

path="/file/path/";
files="/file/path/*";

for file in $files; do {
name=$(basename $file);
echo $name;
newname=$(echo "$name" | sed "s/[0-9]//g")
oldnum=$(echo "$name" | sed "s/[^0-9]//g")

if [ ${#oldnum} -lt 2 ]; then
{
newnum=000$oldnum;
}
elif [ ${#oldnum} -lt 3 ]; then
{
newnum=00$oldnum;
}
elif [ ${#oldnum} -lt 4 ]; then
{
newnum=0$oldnum;
}
fi
newfile=$newname$newnum;
mv $file $path/$newfile;
}
done;

Have a Linux Problem
Linux Forum - Do you have a Linux Question?

Linux Books
Linux Certification, System Administration, Programming, Networking Books

Linux Home: Linux System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.