stupid shell (and UNIX) tricks

I love using abobe photoshop C2’s automation – especially when resizing a series of images for the web. I would love to run linux entirely – but linux keeps falling short in graphics programs (yes, I use gimp – sorry, but Photoshop is better) and printer drivers – linux still can’t run my Canon Pixma pro 9000 color printer. That and every corporation I do work for has some required program or access method that seems to insist on only running on windows.

That said, while all operating systems suck to some degree, Windows consistently manages to suck more than most. Cygwin supplements Windows pathetic command line with a bash shell and a lot of UNIX functionality.

When you run adobe batch scripting to resize a series of jpg’s and then save them in a different directory as gif files, it insists on writing “copy” in the file name. photograph1.jpg becomes “photograph1 copy.gif”. To remove the ” copy” string, run:

$ for i in *
> do
> mv "$i" "${i/ copy.gif}".gif
> done 

sed and awk

One of the instructors I had in UNIX system administration courses was… How to put this… One brick short of a wall. He was supposed to include sed and awk UNIX commands i the course, and he glossed over them as “something you would never use, trust me”. A couple of years later, when I needed and had to learn the commands, I thoroughly realized he was very wrong.

I had to rename a series of website pages in a site redesign – including altering the names inside the pages themselves. Here’s how I did it.

Here’s the command that changed one set of links from the old page to the new:

for i in ` grep photographs_5.html * | awk -F : '{ print $1 }'`
do
echo $i
sed  's/photographs_5.html/photographs_001.html/g' < $i > $i.1
mv ${i}.1 ${i}
done

This loops through and finds any files with the string “photographs_5.html”
echos out each file, changes (using the sed command, standard UNIX tool…) from photographs_5.html to photographs_001.html, creating a new file named [file].1,
then replacing the original file by moving (mv) the [file].1 file over on top of the [file].

I love UNIX. Both of these were done on Windows using the bash shell in cygwin. This supplies basic UNIX functionality to Windows, including the bash shell, sed and awk, ssh and rsync (by selecting the package), wget and gpg (extra package selection), and many other functions. The setup program downloads a list, within which you can select additional packages, download and install.

Cygwin at least makes Windows somewhat command line friendly.

—dsm