Shell Scripting is Useful

22:04 Sun 14 Jan 2007. Updated: 10:22 15 Jan 2007
[, , , ]

The title of this post is meant as understatement.

I’ve been (finally!) moving content from my various old sites to tadhg.com, and shell scripting has been extremely useful for this.

The first task I automated was batch renaming. I had a lot of files that started with “seam”, and I wanted to strip that out of them. This is the shell script I used to do it:

for i in $@
  j=`echo $i | sed -e s/string/replace/`
  echo Renaming file $i to $j
  mv $i $j

It applies the replacement to the files passed to it (e.g. “/.rename.sh *” will apply it to all the files in the current directory). It’s pretty simple, and it relies upon the replacement being something that can be turned into a regular expression—but that is quite often the case. Really, it’s somewhat shameful that I didn’t learn how to do this years ago…

Also for the seamless images I was moving over, I needed to create a lot of slightly-differing HTML files, each doing nothing but displaying the seamless image as a background. The HTML files would also have the same name as the image file, .html instead of .jpg. This is the code for that:

for i in $@
  j=`echo $i | sed -e s/\.jpg//`
  touch $j.html
  k='HTML preceding the image name'$j'.jpg and HTML following the image name'
  echo $k | cat > $j.html

Finally, having brought the content over, I had to point all of the old files to the new location. With domains under my control, that’s pretty easy, as I just use Redirect and RewriteRule Apache directives to do it. But the oldest content, on my Netsoc account (dating back to 1996…), is on a domain where I can’t do that. So I have to insert

(less-than sign)meta http-equiv="refresh" content="0;url=http://tadhg.com/wp/1998/11/30/seamless-backgrounds/"(greater-than sign);

into the head element of every file for that. Here I ended up using Perl, because I found a helpful reference for it. I also think perl is fairly ubiquitous, so I don’t feel too bad that I didn’t learn a ‘real’ shell scripting equivalent for this. I decided to add the meta element above the title element, like so:

perl -pi -e 's/<title>/(less-than sign)meta http-equiv="refresh" content="0;url=http:\/\/tadhg.com\/wp\/1998\/11\/30\/seamless-backgrounds\/"(greater-than sign)\n<title>/' seam*.html

This worked extremely well, and saved me a lots of repetitive pasting. If you ever find yourself having to repeat tasks in a Unix environment, it’s probably worth it to investigate automatic them with shell scripts.

6 Responses to “Shell Scripting is Useful”

  1. NiallM Says:

    Your RSS feed actually takes out the


    in the meta refresh line, so this story is impossible to read via RSS in Safari :-)

  2. kevintel Says:

    Niall – I had no idea you were a Mac user! Or perhaps you went that way long ago, and I never kept up…

    I’m getting more into the command line strategies for handling files, but I was actually going to go to the trouble of writing a PHP script for taking my photos and then organising them into directories by date, and renaming them, using the EXIF data. Could I do this with a shell script? Tadhg, Niall?

  3. NiallM Says:

    Of course. You need the exif module for your PHP script, but it’s pretty easy.

  4. NiallM Says:

    I mean it’s easy with a PHP script, more difficult with a shell script – and you can call PHP from the command line just as well…

  5. kevintel Says:

    Yes, that’s right, I know exactly how to do it all with PHP and thought about the design and execution of such an app, but that doesn’t mean it’s the best solution; do you think the shell script would be a more efficient way of doing it (in terms of file handling and efficiency), or do you think that PHP’s file handling will be close to native anyway?

    My knowledge of PHP isn’t the only reason for why I thought of doing it with PHP, it’s also for the reason that the work/code will be re-usable in a web context (which I’ll definitely make use of). On the other hand, it would be nice to invoke a script from inside the shell and have it as a command-line app.

  6. garret Says:

    shell suits are nice also and they transend the fical geological strata of fashion.

    “Always same, always same…. sports casual….. always same.”
    Goes the hiko!

    Tadhg, check out this article written by Zadie Smith for the Gaurdian, it provides possible answers and numerable questions all of them worth while!

Leave a Reply