01Jan

Stupid Gawk Trick

Posted by Elf Sternberg as Linux

This morning, for no reason that I’d care to discuss in public, I needed to rename every file in a directory to the index number of its position in the directory in asciibetical order, and add an extension.

The harmless version of this was:

ls | gawk 'BEGIN { c = 0; } c += 1 { print "mv", $0, c".jpg" }'

The version that actually does the work:

ls | gawk 'BEGIN { c = 0; } c += 1 { print "mv", $0, c".jpg" }' | bash

Better version (harmless):

ls | gawk '{ printf ("mv \"%s\" %02d.jpg\n", $0, NR) }'

Don’t use the second unless, well, you know, you intend on doing it.

Comment Form

Subscribe to Feed

Categories

Calendar

January 2010
M T W T F S S
« Dec   Feb »
 123
45678910
11121314151617
18192021222324
25262728293031