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.
no subject
Date: 2010-01-02 04:57 am (UTC)no subject
Date: 2010-01-02 04:15 pm (UTC)Q: It's been a few years since I've used Unix/Linux seriously. I *think* I recall a program will tell you the TYPE of a file. Is that still in there?
no subject
Date: 2010-01-02 05:29 pm (UTC)As in:
"List all files in reverse temporal order; feed that list to 'file', which will spit out the filename and type. Include only those that are JPEGs, cut the filename out of the stream, then feed that list of names to a program that will show them to me in a manual slideshow."
In case you're wondering, this is a great way to ripe your Firefox cache and find "that batch of images you were just looking at," in their native form.
no subject
Date: 2010-01-03 01:40 am (UTC)