Archive for the 'Scripts' Category

Why Does BASH Differ Across Different Platforms?

Using the BASH shell in OS X I wanted to examine the EXIF data of my JPG files for a script I am writing. Unfortunately, the exif command in BASH does not work, despite there being a man page for it. I’m solving this problem by using ExifTool by Phil Harvey, but it leaves me asking the question “Why does the BASH exif command work with Linux but not with OS X?” I had a similar problem using the uniq command that works fine with Linux but only partially with OS X:
uniq -w 32
That is because in OS X there is no -w flag. Most of the scripts and examples I find to learn from are written in BASH with Linux and I’m having a really hard time understanding why BASH is not the same with OS X. It makes no sense.

Anyone?

UPDATE 10/07/08

In fact, you don’t get exif on Linux so what am I talking about (and there isn’t even a man page for it like on OS X). Use exiv2 or exiftool. Still confusing why it’s there in OS X though..

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.

Forwarding URLs & Their Descendants - RedirectMatch & .htaccess

If you want to redirect a whole site and all descendant pages to another URL, you can put this in your .htaccess file in the root of your server:

RedirectMatch ^/ http://en.wikipedia.org/wiki/TV_Links

Took me a while to ensure not only www.blah.com and http://www.blah.com were redirected, but also blah.com and blah.com/foo/bar and so on. This technique seems to work well in these instances but I have yet to master regular expressions so if any one has any pointers on trimming the end of the URL (for example ensuring http://www.blah.com/something?glass=halfempty does not become http://www.urlforwardedto.com?glass=halfempty), I would be very greatful.

Next Page »