Question: I have many hundreds of thousands of files in a directory. These files are named as follows: 

```
left-00001.tiff
left-00002.tiff
...
left-99999.tiff
left-100000.tiff
...
left-245000.tiff

```

I would like to rename the files as follows: 

```
left-000001.tiff
...
left-099999.tiff
...
left-245000.tiff

```

I found elegant solution to this problem [here](https://stackoverflow.com/questions/5417979/batch-rename-sequential-files-by-padding-with-zeroes).

This solution implements a bash script called `zeropad.sh`. the bash is coded as follows:

```
#!/bin/bash
num=`expr match "$1" '[^0-9]*\([0-9]\+\).*'`
paddednum=`printf "%06d" $num`
echo ${1/$num/$paddednum}

```

and can be applied iteratively using `for loop` as follows:

```
for i in *.tiff;do mv $i `./zeropad.sh $i`; done

```

However, this solution takes a very long time because it does much unnecessary work renaming all the files which are already properly padded. `i.e. as %06d type numbers`. For my own purposes this is solution is very slow. 

I have two questions: 

1- How can I modify the iterator to only apply `zeropad.sh` on files which need to be zero padded? 

2- How can I use the command `touch` in a `for loop` to generate test data? It is crucial to verify that this script works before I apply it on the original data.

Answer: 
The bulk of the time spent by your loop is probably in calling your `zeropad.sh` script.

Instead, do it all in one script:

```
#!/bin/bash

for filename in left-*.tiff; do
    if [[ "$filename" =~ ^left-0*([1-9]?[0-9]+)\.tiff$ ]]; then
        num=${BASH_REMATCH[1]}
        newname="left-$( printf '%06d' "$num" ).tiff"
        if [ "$filename" != "$newname" ] && [ ! -e "$newname" ]; then
            echo mv "$filename" "$newname"
        fi
    fi
done

```

Remove the `echo` once you have verified that the script is doing the correct thing.
You can rename all the files in parallel. Do the following trivial changes on the same slow code that you had provided in your question, as follows:

```
cd data_folder  # cd the folder where you put the *.tiff files
for i in *.tiff;do 
{
mv $i `./zeropad.sh $i`;
}&

```

This will rename all the files at once. Please be aware that you must have enough memory resources on your workstation before you run this code inside the folder that contain the `*.tiff` files. No enough memory resources may lead to memory crash. But given that the process is only renaming files you should be fine!

In order to take into acccount memory resources on your workstation. Save the following code in a file called `code`, give it permissions then run it:

```
mem=$(free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }' | grep Memory | awk '{print $3}' | tr -d "()%MB" | cut -d / -f 2  )

for i in *.tiff;do 
{
mv $i `./zeropad.sh $i`;
}&

if [ $mem -lt 100000 ] 
 then 
   if  (( "$i" % 75 == 0 ))
    then 
      sleep 4
   fi
 fi
if [ $mem -gt 100000 ] 
 then 
   if  (( "$i" % 300 == 0 ))
    then 
      sleep 3
   fi
 fi
done

```

When you run `code`, it will check memory resources on your workstation using the variable `mem`. If memory is less `100000MB`, then it will rename `75 files` at once. If memory resources are more than `100000MB`, then it will rename up to `300 files` at once. However, you can adjust all the variables as you want.