Bash manipulation on sequences.

I’m going to come at you today with a very common situation. Renaming a series of files, bash and GNU utils style. This applies to something as trivial as importing my camera photos which typically gives me a bunch of files named IMG_2042.JPG, IMG_2043.JPG, IMG_2044.JPG and so on. I’m here to show you how to deal with that.

We’re gonna tackle this ground up. Firstly, I need a way to increment a number. seq is our tool.

seq -f %04g 1 3
0001
0002
0003

Sexy huh? We’ve already have a major building block right here. Let’s dissect this quickly. The ‘-f’ tells seq that we want a specially formatted list of numbers. In our case, zero padded numbers, at least 4 wide (%04). The letter ‘g’ gets replaced the current number in the following range (1 thru 3 in our case). If you don’t have padded numbers you can omit the use of ‘-f %04g’ altogether. The last two arguments are the range to start and end.

seq 1 3
1
2
3

Sweet, now we have way of representing pretty much any sequence of numbers, thanks seq! Now let’s tackle looping over this sequence of numbers and use them for renaming using a simple ‘for’ loop. In our case below the letter ‘n’ will get the results of the seq command.


for n in $(seq -f %04g 2042 2044) ; do mv IMG_$n.JPG utah.$n.jpg ; done
`IMG_2042.JPG' -> `utah.2042.jpg'
`IMG_2043.JPG' -> `utah.2043.jpg'
`IMG_2044.JPG' -> `utah.2044.jpg'

That one-liner is so unbelievably flexible (replace `mv’ with `cp’ or `ln’ for instance) it will probably be your number one oft used idiom while working with bash in production. As a matter of fact, pretty much every thing I do is looping over a list of things – yum, sounds lispy.

I’ve always found this to be a great (re)read. In the beginning was the command line. It’s a zip archive, with an ol’ fashioned text file for you to read authored by Neal Stephenson.

Getting to second base.

Preparing to take a picture from second base at Yankee's Stadium

Well, I never played anything but softball when I was in 2nd grade or something, yet somehow I’ve managed to find myself on holy ground at second base in the new Yankee’s stadium. And the majority of my teachers never thought I’d get anywhere (except for Judy Hemphill).

If you’re wondering WTF, well let me explain. Apparently you can’t actually make a commercial starring the Yankees and film at the stadium. You have to take an awesome ferry to Staten, shoot them there and fill in the stadium later in post. So, somehow, I got to be the lucky guy let onto the field to take reference photography. Unfortunately, I couldn’t take a tri-pod out with me as the turf is worth more than the GDP of small countries or something.

Photo credit : Al Torres

New Job Description

A stressed out squirrel

ONDCP "Tree Service"

Hi so what’s new with me? Well, I quit my post as CG Supervisor at Method NY and I’m going back to being a freelancer. I’ve had a very proud 2.5 years there. Leaving was like breaking up with a dozen people at the same time. I’ve learned a hell of a lot, and I’ve also made some mistakes, but I’ll be better for them next time.

In the short term I’ll be starting my first gig at Mass Market. Also I’ll be cutting an updated reel – featuring the squirrel pictured above. The squirrel, fence and background is entirely computer generated (with Houdini) and wants you to not do drugs.

Update 2010-12-14
This spot has finally been released.
http://adgallery.whitehousedrugpolicy.gov/media/p/402.aspx