Jeremy Richard moving pictures editor Est. 2008

$X+1

using Bash on OS X 10.9

With a shell for command lines to prosper, Bash can use the power of looping to cause the change of state of a series of files in one go.

For the following explanation we are assuming that we have in hands a series of tiff files all inside a folder called ‘tunnel’. And we wish to rename and renumber these files as tunnel_0001.tif, tunnel_0002.tif, tunnel_0003.tif and so on.

Process

  1. Firstly, let us create a new text document using TextEdit, then copy and paste in it the following code:

    #!/bin/bash
    X=1;
    for i in *.tif; do
    mv $i tunnel_$(printf %04d.%s ${X%.*} ${i##*.})
    let X="$X+1"
    done
  2. save the document as renumber.sh (or anything else as long as there is a .sh extension) and place it inside the folder where the images we want to rename are;
  3. open the Terminal and execute the following commands:
    1. move to the desired folder: cd /*path*/tunnel;1
    2. once in it run our bash code: sh renumber.sh
  4. We won’t see anything in the Terminal. Once the process done (more or less long depending the amount of files) it will simply jump to the next line waiting for eventual new instructions.
Screenshot of the OSX Finder
A bash file which will renumber all the .tif files it can find.

Explanation

Let us go through what we just did:

  1. We first created a program using the bash language. Without going into details (as I am far from being an expert in bash) here are the important bits:
    • #!/bin/bash more or less tells where the bash working environment is in our machine for our batch to work and prosper;
    • X=1 means that we want to renumber our sequence starting with 1. If we put something like X=34, the renumbered sequence will start with the number 34;
    • for i in *.tif gives a value to a variable called ‘i‘. Indeed, as we don’t want to apply the action on each file one by one, under ‘i‘ we group every files (*) having the tiff extension (.tif) together;
    • the biggest line of the program is where the main action happens: it uses the mv (move) command that will change the old name of the files $i (the dollar sign is used to called a variable) to a new one having with a base name tunnel_ (which can be changed), four placeholders %04d (which can also be changed);
    • let X="$X+1" tells the program to add 1 to the number of each new file renumered. Without it, it would simply renumber every files with the number 1, which in the end means that we would have just one file as our OS won’t accept two files with the same name.
  2. we then saved our action into a sh file which is an executable file, some kind of .app or .exe but which works only in a certain type of environment where its content can be understood and interpreted. In this example the environment in question is bash (a Unix shell), hence the #!/bin/bash line in the file;2
  3. finally we execute what is inside the sh file by using the sh command (is like when double clicking on an .exe file).

The End