Stupid Gawk Trick
Jan. 1st, 2010 04:04 pmThis 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.
This entry was automatically cross-posted from Elf's technical journal, ElfSternberg.com
no subject
Date: 2010-01-02 12:09 am (UTC)Love that, "asciibetical"..
oh, RIGHT. If you hand this off to somebody else and you really want to make sure it's asciibetical as opposed to alphabetical (case-insensitive) you need to do
LANG=C ls....
'cause if you don't and some nit has
LANG=en_US
they will get "fred" comes before "George", which isn't what you said.
At least on Linux. No clue what BSD does, don't really care right now but still. Worth knowing, just for completeness.