Here, I'll show you a mind-reading trick. Think of a number between 1 and 3.

Obscure Linux Commands: Some of My Favorite Incantations

Date/Time Permalink: 03/13/08 07:11:09 am
Category: HOWTOs and Guides

A little random grimoire of my spiciest magic spells, which don't fit well anywhere else:

emacs -batch -l myLispProgram.el

What it does: It runs Emacs in batch mode to run whatever Lisp code you might have in "myLispProgram.el". Just like Python or CLisp, it will process the code, write its output, and quit - without starting up the editor. The output that would normally print in the minibuffer in Emacs will just dump to screen instead.

True Lisp programmers will scoff, "So just run CLisp programs", but I've never had much use for getting deep into Lisp - I find that it's mainly handy to use the eLisp dialect for those few jobs that are too clumsy for Bash scripting. The most common situation is when I'm coding in another language and have to make a whole bunch of lines with small differences, such as processing frame files (numbered frame_00.png, frame_10.png, etc.) into an animation.

convert image.jpg -resize XX% image.jpg

What it does: It's the fastest way to resize an image, that's all. Useful for generating thumbnails of an image gallery. The 'convert' command is part of the Image Magick suite. A script example:


for FILE in $(ls *.png)
  convert $FILE -resize 160x120 ./thumbs/$(echo $FILE | sed 's/png/jpg/')

exit 0

Which generates JPG thumbnails from PNGs and saves them to a separate 'thumbs' folder. So the -resize option to 'convert' will work with either a percentage (50%) or a specified size in XxY format. It beats firing up the Gimp, especially for one screenshot.

WORDLINE=$((($RANDOM * $RANDOM) % $(wc -w /usr/share/dict/words | awk '{print $1}')))"p" && sed -n $WORDLINE /usr/share/dict/words

What it does: It gets a random word from the dictionary. Of course, once you get this line down, you'll want to call it from a script named something like ''. Useful in generating word games or puzzles, starting a Markov chain, or getting a random word for a CAPTCHA program. The same method can also be used to get a random line from any file, with the substitution 'wc -l' to word-count lines instead of words. Since each word gets in own line in the Unix dictionary file, the difference is indistinguishable in this case.

Simply calling 'sed -n 100p $FILE' would print the 100th line of that file, see, so our only remaining job is to get a random number that has a range up to the number of words in the dictionary, but not higher. Then it's easier to compose it into a variable with a 'p' appended onto the end, since sed chokes parsing "$WORDLINE p".

Anybody who has a shorter line is welcome to post in the comments. I sense a Perl ninja about to strike...

echo -e '\E[7m'

What it does: It turns on reverse video mode for the console session. Turn it back off with:

echo -e '\E[27m'

This is useful for highlighting your next command (chained on with the handy '&&' conjunction) so the output is visible in reverse coloring. If you have long terminal sessions where you have to scroll back to see some output and don't want it lost, this comes in handy. Of course, if you already have scads of escape-sequence hacks in your Bash $PS1 prompt string as I do, turning off inverse won't be an issue. It's also easily aliased in your .bash_profile - for example

alias invon="echo -e '\E[7m'"

will make 'invon' the command to use to do this.

for X in $(seq 2 12); do for Y in $(seq 2 12); do echo -ne $(($X * $Y))"\t"; done && echo; done

What it does: Prints out a multiplication table. Learn your math, junior!

COOKIE="16i"$(mcookie | tr 'a-z' 'A-Z')"p" && dc -e $COOKIE | cut -b -12

What it does: Earlier, you saw me generate a larger random number by multiplying $RANDOM times itself. Here's a way to get an even longer random number - featuring the rarely-encountered 'mcookie' command, running it through tr to make the digits uppercase so dc doesn't choke, sticking dc's whole command line into the variable, and having dc translate it from hexadecimal to base ten.

Of course, you'll overflow your buffer if you try to do any math on a raw md5 value in Bash. So that's what the 'cut -b -12' does is only preserve the first 12 digits of the number. Vary that for a longer or shorter number as needed.

Linux Punx tagged this wall with graffiti.

Follow me on Twitter for an update every time this blog gets a post.
Stumble it Reddit this share on Facebook

suddenly the moon