elfs: (Default)
[personal profile] elfs

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.

This entry was automatically cross-posted from Elf's technical journal, ElfSternberg.com

Date: 2010-01-02 12:09 am (UTC)
ext_3294: Tux (Default)
From: [identity profile] technoshaman.livejournal.com
That's damn useful, along with variants created by varying arguments on ls(1)... -c, -t, -r, -S, -U (if you're doing filesystem weirdness), etc...

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.

Date: 2010-01-02 04:57 am (UTC)
From: [identity profile] http://users.livejournal.com/_candide_/
When it comes to my script-fu, I always find it better to print out a list of commands and pipe or tee it to a file, to be sourced later. Piping directly to a shell is just asking for tears and wailing.

Date: 2010-01-02 04:15 pm (UTC)
From: [identity profile] mouser.livejournal.com
That is an... odd need for a script.

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?

Date: 2010-01-02 05:29 pm (UTC)
From: [identity profile] elfs.livejournal.com
Sure, it's file.

As in:

ls -t | xargs file | grep JPEG | cut -d':' -f1 | xargs qiv -fnt


"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.

Date: 2010-01-03 01:40 am (UTC)
From: [identity profile] mouser.livejournal.com
Okay - just checking. As I said, it had been a while.

Profile

elfs: (Default)
Elf Sternberg

December 2025

S M T W T F S
 12345 6
78910111213
14151617181920
21222324252627
28293031   

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jan. 2nd, 2026 06:09 pm
Powered by Dreamwidth Studios