Archive for the ‘linux’ Category

Fedora Bash History Meme

Wednesday, April 16th, 2008

I just learned about a Bash History Meme originated at Planet Fedora from a blog I read regularly.

In short, you run the following command in bash, and show your results.

history | awk '{a[$2]++ } END{for(i in a){print a[i] " " i}}'|sort -rn|head

My results from my home box are as follows;

103 ls
59 cd
52 exit
38 vi
38 firefox
29 for
22 su
18 /opt/hdv/myworkflow.sh
14 ssh
13 diff

Seems I’m unwilling to simply close my terminal window in KDE, but instead have to type “exit”. The “myworkflow.sh” script is a work in progress which I hope to one day post about. It does stuff to help me convert and do post production on video from my HDV camcorder. I’ve been using “diff” to compare original files with modified ones for web updates I’ve been doing for clients.

Here’s the results from my web hosting box;

394 ls
212 cd
89 vi
77 exit
60 du
30 mv
28 rm
20 mkdir
19 pwd
12 cp

Suppose there’s nothing particularly interesting here. I was checking a file upload progress in a very nasty way by calling “du”, what can I say.. I’m lazy.

Tools for updating an online image gallery

Thursday, March 6th, 2008

Not long ago I shared my bash script for creating thumbnails and manageable sized “big” images for an image gallery, or other image store. Well, today I had to update an existing gallery with a pretty significant number of new images. It occurred to me that I’d like to only convert new images rather than re-converting the whole lot. Then, I’d like to simply upload the new files to my server in some automated way.

So here’s the tools to do it. It assumes you have the same layout for your pictures on your local drive as I do, which is as follows.

  • RootDirectoryForGallery — This contains the full resolution pictures directly from your camera, and the following directories.
    • web — This is a sub directory of RootDirectoryForGallery which contains the “big” web version of your image, and the thumbnails directory described below.
      • thumbnails — This is a sub directory of web, and contains the thumbnail sized versions of the images.

The first step, is to copy your new pictures from the digital camera to your RootDirectoryForGallery. Then, we want to get a list of the files that are new, so we run the following from the RootDirectoryForGallery.

diff . web/ | grep 'Only in \.: d' | awk '{print $4}' | grep jpg >> 3-6-08_update.diff

This creates a file in the RootDirectoryForGallery named “3-6-08_update.diff” which contains a list of only the new pictures, one per line. Now, we want to use our image thumbnail script to convert only the new images to the “big” and thumbnail sized images. I discovered that I needed to make a change to my script in order to do this. Namely in the for loop I needed to enclose the command used to list files in “$()”. The modified script is shown below. Notice the “$($SEARCH)” where there was previously just “$SEARCH”.

#!/bin/bash
 
SEARCH=$1
SIZE=$2
DEST=$3
 
if [ $# -lt 3 ]
then
echo "You must pass three arguments 1) The search string (usually *.jpg) 2) The destination size I.E. 500x374 3) The destination directory"
exit 1
fi
 
for i in $($SEARCH)
do
        echo "Converting $i"
        convert -resize $SIZE $i -resize $SIZE +profile '*' $DEST$i
done

So with our edited script we convert the files.

/opt/imagethumbnail.sh "cat 3-6-08_update.diff" "800x600" "web/"
/opt/imagethumbnail.sh "cat 3-6-08_update.diff" "150×112 "web/_thb_"

Lastly, we want to upload just the changed files to our server. We’ll use rsync for this. The paths, server name, and user name have been changed to protect the innocent. ;-)

rsync -e ssh -av /path/to/local/copy/ yourusername@yourserver.com:/path/to/www/server/copy/

And there you have it, with just a few simple commands you’ve created thumbnails of all of your images, and uploaded just the new ones to your server. Easy!