bash tricks

See also Advanced Bash Scripting Guide

Magical Keystrokes

  • Up-arrow / down-arrow : cycle through command history
  • Tab - completes command names if on the first word, filenames thereafter
  • Ctrl-a : moves cursor to the beginning of the line.
  • Ctrl-e : moves cursor to the end of the line.
  • Ctrl-k : delete from cursor to end of line
  • Escape,backspace : delete word before the cursor
  • Escape,d : delete word after the cursor
  • Escape,> : repeat last used argument

Scripting

Operating on multiple files/dirs/whatever, using brace expansion
For cases when using the glob (*) is not quite right, do something like: rm abc{01,03,05,07,10}.jpg
Simple loop
The above could also be done like this, although this is less efficient in this example.:
for i in 01 03 05 07 10 ; do rm abc$i.jpg ; done
Another example:
for i in 1 3 7 ; do convert -geometry 640x480 -quality 90 abc0$i.jpg abc0$i-sm.jpg
Or to do them all:
for i in *.jpg ; do convert -geometry 640x480 -quality 90 $i `basename $i .jpg`-sm.jpg
There's nothing special about numbers, or files:
for i in calypso cpu02 cpu03 cpu04 cpu05 cpu06 cpu07 cpu08 ; do ping -c 1 $i ; done
Counter loop
Say you have a folder with img1000.jpg through img1100.jpg and you want to delete img1010.jpg through img1050.jpg:
i=10; while [ i -lt 51 ] ; do rm img10$i.jpg ; i=$((i+1)) ; done
Note that with numbers, the zero-padding is kind of a pain. Also, you don't actually have to use the numbers in file names; you can just use it to do something 50 times.:
i=0 ; while [ i -lt 50 ] ; do date ; i=$((i+1)) ; done