Quickly Rename Multiple Files Using BASH & Regular Expressions And Rename Files Based On EXIF Information Using ExifTool

I wanted to rename a number of JPG files in a directory and I wanted to use BASH as it is amazing powerful yet I don’t understand it as well as I want, so I took the time to find a method shown on numerous forums and sites and then understand what it is doing. Here is the script, or line I used:

for f in `find . -name ‘*.JPG’` ; do mv $f ${f/DSC/ODSC}; done

To break it down:

for f

This starts a for loop with the variable f, also refered to as $f (the $ means variable)

in

This sets the value of $f to be the output of:

'find . -name '*.JPG'' ;

Which will find and return filenames which adhear to the expression *.JPG (* is used as a wildcard and means any number of any characters) so blah.JPG, DSC0001.JPG, owt.JPG and so on will all be returned.

do mv $f ${f/DSC/ODSC};

Will rename the part of the filename inputted, in this case the variable $f (which is set to what ever find returned), to something else. In this example it is replacing any instance of DSC with ODSC. By preceeding mv with the BSD general command do, mv can be executed from within the running shell process which is this script.

done

This will end the for loop.

I have used various types of loops in PHP and a few other languages and in BASH the following is relevant and very useful to know when structuring a loop:

SYNOPSIS
for start test next body

The for command first invokes the Tcl interpreter to execute start. Then it repeatedly evaluates test as an expression; if the result is non-zero it invokes the Tcl interpreter on body, then invokes the Tcl interpreter on next, then repeats the loop. The command terminates when test evaluates to 0.

Entry for ‘For’ from the BASH Manual

As such, the script I have used can be split into these 4 components of start, test, next and body and indeed I find it easier to understand the process and what each stage of the loop does in this manner.

BASH IT UP.. Now I need to write a script which renames all JPG files in a folder to *0001 sequentially. Or to extract date and time information, order the files oldest to newest and then rename the files in that new order sequentially. That would be tops for sorting out photos from my phone back to date order and have no filename conflicts.

So here it is:

exiftool "-FileName<CreateDate" -d "%Y%m%d_%H%M%S.%%e" /Volumes/Media/K800

And the output is in this format: 20080121_185309.JPG

Using ExifTool by Phil Harvey (you legend) which has very powerful date formatting abilities you can read the EXIF Creation Date field from a file and rename that file with that information, or in this case a formatted version of it. Saves using sed which is great and works very well in a whole directory.

Anton Chekhov Quote From Uncle Vanya

http://invision-images.com/archive/latest%20stories/sochi/INV-GAL-008/preview

Who but a stupid barbarian could burn so much beauty in his stove
and destroy that which he cannot make?
Man is endowed with reason and the power to create, so that he may
increase that which has been given him, but until now
he has not created, but demolished.
The forests are disappearing, the rivers are running dry,
the game is exterminated, the climate is spoiled,
and the earth becomes poorer and uglier every day.

An excerpt from the play Uncle Vanya by Anton Pavlovich Chekhov published in 1889.

[Taken from the Gutenberg Project translation from Russian to English by Marian Fell in 1913].

This originally started as a post solely for this quote (as I think it is very evocative) but upon realising the quotes relevance not only to UK and worldwide stances on how people live on and with the earth, but specifically with the rate of deforestation in Anton Chekhov’s home country of Russian I feel I should expand.  Learn more here: http://www-pub.naz.edu:9000/~jwitten2/about2.htm

Someone Please Give Me A Job

It is very rare that I find a job for which I can apply with the qualifications I have. This is so very frustrating and demoralising. Worse is that twice in the last year jobs I have applied for have been withdrawn after my application has been accepted, once after I attended an interview and got a call offering me the job.

Someone please give me a job and stop making my life a misery. Please.

Thank you for your application for the above post. However I regret to inform you, that due to unforeseen circumstances this post has been withdrawn.

Please accept my apologies for any inconvenience caused by this decision.

« Previous PageNext Page »