------------------------------------------------------------------------
Renumber files after deleting a few files
------------------------------------------------------------------------

The problem was posed by Tuhin Ghosh, DAA, TIFR.  "I have a list
of folders with numbers as names (say, 1 to 10). Now I delete folder
5 and 7. How do I rename the remaining folders such that the names
are now from 1 to 8?"

$ touch {1..6}.rat

$ rm 4.rat

$ ls -1 *.rat
1.rat
2.rat
3.rat
5.rat                #note 4.rat is missing
6.rat

Our task is to create a command "mv n.rat m.rat" where
n=[1,2,3,5,6] (as above) and m=[1,2,3,4,5]

Using "nl" we can generate m=[1,2,3,4,5]

$ ls -1 *.rat | nl -n ln |awk '{ext=$2;sub(/^..*\./,"",ext);print "mv",$2,$1"."ext}'
mv 1.rat 1.rat
mv 2.rat 2.rat
mv 3.rat 3.rat
mv 5.rat 4.rat
mv 6.rat 5.rat

In "ext" we infer the extension and then construct the "mv old.ext nl.ext" where nl
is provided by "nl".

Now that you are confident you can execute by piping to "sh -x"

$ ls -1 *.rat | nl -n ln |awk '{ext=$2;sub(/^..*\./,"",ext);print "mv",$2,$1"."ext}' | sh -x 

You can be clever and only execute those commands where a rename is necessary

$ ls -1 *.rat | nl -n ln |awk '{ext=$2;sub(/^..*\./,"",ext);b=$1"."ext};$2!=b{print "mv",$2,b}'