Important: this article is not released under a Creative Commons license and may not be copied or redistributed in any form. Please use our contact page to get in touch with us if you wish to reproduce the article.

Lets give it a bash


Allan Shand's Gravatar

Allan Shand
published July 7, 2011, 3:39 p.m.


Hello everyone (mum) this is my first post for 2buntu. You could probably guess this after reading the first few lines its not exactly Shakespeare. By this time you might be wondering what I'm actually doing here as I have been for most of the last 24 Hours. However never fear I do have something to write about other than the weather today which BTW was a scorcher where I am.

The topics I will cover are closest to my work which is in graphic design and online commerce. <ul> <li>Some of the best programs for making efficient use of your time (no Flash)</li> <li>Bash scripts that will make your RSI hospital bills smaller Don't worry it won't be all code I have no fu just a burning desire not to waste time doing monkey work on a <span style="text-decoration: underline;">COMPUTER</span></li> <li>Some tips, tricks and tutorials that I have found in my travels over the years (as a lonely pilgrim)</li> <li>I may even get all ephemeral and talk about the future of open-source but you can safely ignore these as they will be complete fantasy.</li> </ul> I use Ubuntu everyday for my work so if something doesn't work or is inefficient it has to go. This is why the Hard disk that I had Vista installed on is currently being used as a toy by my Daughter. I however am not a programmer so generally I use GUI programs but now and again it's just not efficient to do something this way . Eg resizing 500+ images if you have to do this individually with the gimp some time in rehab will follow soon.

The example I will share with you today is a script I was working on today which saved me at least 3 hours giving me the time to write this post for better or worse. The situation was this I had 200 images which all needed to be in different directories ahhhh copy paste nightmare this was the solution. <pre class="brush: bash; gutter: true">#!/bin/bash

SOURCEDIR=/home/allan/Documents/Patterns/BPBRough/roughlisting

find $SOURCEDIR -type f -iname 'bpbrough.jpg' | while read SOURCEFILE; do DIRECTORY1="${SOURCEFILE%bpbrough.jpg}/" DIRECTORY2="/home/allan/Dropbox/Patterns 1.0/BPB ROUGH PDF/${DIRECTORY1#/home/allan/Documents/Patterns/BPBRough/roughlisting/}" FILE="${SOURCEFILE#/home/allan/Documents/Patterns/BPBRough/roughlisting/}" DIRECTORY="${DIRECTORY2}${FILE}" mv "$SOURCEFILE" "$DIRECTORY" done</pre> I'm sure many programmers will weep at the inefficiency and the lack of comments but I only finished it this afternoon and it works so sue me...... Soooo a quick explanation of what is happening, in the third line we add the directory we want to search to a variable (easiest way to get this is to copy and paste from the address bar in nautilus) and in the fifth line we are doing a search on our chosen directory and passing the results one at a time to a While do loop, in the variable SOURCEFILE is the full URL of each file found in the search. <pre class="brush: bash; gutter: true">SOURCEDIR=/home/allan/Documents/Patterns/BPBRough/roughlisting? find $SOURCEDIR -type f -iname 'bpbrough.jpg' | while read SOURCEFILE; do</pre> The sixth and seventh lines are cutting up the original directory and providing an alternative directory. In the sixth line we cut the end off of our file name using the % sign to show we want to remove from the end rather than the beginning and we replace it with a / making this a directory (it just so happens that we use unique codes in the file names and directories which match good tip if you want to save time). In the seventh line we replace the original address with the one we actually want to move the files to as we are removing from the beginning we will be using the # symbol instead of the % sign (believe me it took a while on google to track that nugget down!!!. <pre class="brush: bash; gutter: true">DIRECTORY1="${SOURCEFILE%bpbrough.jpg}/" DIRECTORY2="/home/allan/Dropbox/Patterns 1.0/BPB ROUGH PDF/${DIRECTORY1#/home/allan/Documents/Patterns/BPBRough/roughlisting/}"</pre> The last few lines put together our new destination directory and the name of the file that should be moved in this loop and move it using the mv command obviously you may want to use the cp (copy) command instead of mv but I'm not going to do everything for yah!!!. <pre class="brush: bash; gutter: true">FILE="${SOURCEFILE#/home/allan/Documents/Patterns/BPBRough/roughlisting/}" DIRECTORY="${DIRECTORY2}${FILE}" mv "$SOURCEFILE" "$DIRECTORY" done</pre> And that raps it up for this post I promise the next one will have lots of pictures and hardly any writing as I have used my entire vocabulary budget on this post.......