Discovering some of Linux Cow Powers: awk and bash
Tuesday, July 22nd, 2008
If you’ve never used linux you should give it a try. The simplest way is to install colinux with xming. If you feel its involved, just burn an ubuntu 8.04 bootable cd. It works out of the CD with almost all the every day used features without installtion, formating or any other extra work.
One of the cow powers that I use linux for, and didn’t find a way to do it using the windows shell is renaming a lot of files. I had around 3000 bmp files, their names were in the following format:
<word>-<volume>-<number>.bmp
and I wanted to rename them to
<word>-<number>.bmp
I couldn’t find a way to do this on windows except with writing a small pogram. On linux the solution is a program called awk, which can tokenize input strings, and write parts of them out. awk is a sophistiacated tool, and I usually use a very small subset of its features.
The solution was finally something similar to this:
find .bmp | awk -F- '{print "mv "$0" "$1"-"$3;}' | bash -v
The first part is running the find program to find all files with extension .bmp. The results are then piped as input to awk, instead of being written to the screen. awk is told that the field separator is ‘-’ using the -F parameter. A one statement awk program says print the words mv to issue commands to rename a file, then $0 for the full string before tokenizing, then the $1 for the first tokenized part, and $3 for the third tokenized part, skipping the second. So awk is being used to write the commands that we need to execute.
Those commands are then piped to bash, the bourne shell with the verbose flag on so that we can see the commands being executed.
A good practice when trying something like that is to pipe the results first to less, instead of bash. less is a program like more, which gives the user the ability to inspect whatever was piped to it and filp forward and backwords. It also includes some nice features like -N to show numbers, and searching for a word using the forward slash / , with commands very similary to vim.
So the less version of the line whould be this:
find .bmp | awk -F- '{print "mv "$0" "$1"-"$3;}' | less
This is a very handy technique in renaming lots of files, or carrying batch operations on multiple files that I usually use.
Technorati Tags: linux, bash, find, awk, less, rename, move, mv