Renaming Files

I had an interesting conversation the other day about renaming a lot (thousands) of image files.

The story…

The person was a photographer, and had been using his camera for years. Every photo taken by the camera got a sequential number/name:


and so on. Upon uploading the images to his computer, he gave each photo a more descriptive name using some unmentioned software. What it did was append some text to the end of the filename before the extension. If he selected multiple files (control-click or shift-click), it would append the same text to all the selected files. His directory now looked like this:


For whatever reason (I got a long, convoluted reason that didn’t quite make sense), he needed to rename each and everyone of the files so the leading zeros were removed, and the “DSC” was changed to a “NHH” (his initials).

His solution was to sit down at the keyboard and start typing away. [F2] to select the filename for editing, left-arrow to move the cursor to the beginning of the filename, [DEL] to remove the first three letters, [N][H][H] to add the new ones, [DEL] 1-5 times to remove the leading zeros.

ICK! That is 13 keystrokes per file! And he literally has TENS OF THOUSANDS of files! If he could do one every two seconds and work non-stop without making a mistake, that is 12 HOURS of typing!


Here is the single line in bash I gave him that accomplished the exact same thing in a matter of seconds:

i=0; for file in *; do let i+=1; mv "$file" "NHH${i}${file:9}"; done

The output is now:


just as he wanted it. Much easier, huh?

Let that be a lesson to you: in the end, even if it had taken him a full 8-hour work day to learn, research, practice, and code that single line, he still would have come out way ahead on time over doing it manually.

Enter the obligatory XKCD comic that covers this exact topic. Is It Worth The Time?

Tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.

Protected by WP Anti Spam