Penguin

Sort a directory of MP3s into directories like Artist/Album Name/

for file in *.mp3; do
    eval $( id3tool "$file" | sed 's/^[^:]* //; s/:[\t ]*/="/; s/[[:cntrl:]]//g; /./!d; s/ *$/"/' )
    { [ "$Artist" -o "$Album" ] && dir="$Artist/$Album/" ; } || { [ "$Artist" ] && dir="$Artist/" ; } || continue
    mkdir -p "$dir" ; mv "$file" "$dir/"
done

It uses id3tool to read the ID3? tag, instructs SED to massage the information into something that looks like Shell script, executes that to put values into variables, checks which variables are set, and if at least an artist name (optionally also album name) is given, moves the file into that directory. (It's rather a mouthful for a oneliner, but hey.)


Refresh the all GPG keys from the KeyServer without flooding it

for uid in $( gpg --with-colons --list-keys | grep ^pub | awk -F: '{print $5}' ) ; do gpg --refresh $uid ; sleep 5 ; done
Alter the number after sleep(1) to change the speed of the refresh. This is equalivent to the slightly more understandable
for uid in $( gpg --with-colons --list-keys | grep ^pub | awk -F: '{print $5}' ) ; do
    gpg --refresh $uid
    sleep 5
done

Notes by AristotlePagaltzis:

This is a useless use of grep(1). Better written as follows
for uid in $( gpg --with-colons --list-keys | awk -F: '/^pub/{ print $5 }' ) ; do gpg --refresh $uid ; sleep 5 ; done
Correcting self: this is still a useless use of backticks. As with most for f in $( ... ) loops it is better written with a while loop
gpg --with-colons --list-keys | awk -F: '/^pub/{ print $5 }' | while read uid ; do gpg --refresh $uid ; sleep 5 ; done

This applies to seq(1)-based loops too -- seq 1 10 | while read i is better than for i in $( seq 1 10 ).

This could be abbreviated as

gpg --with-colons --list-keys | awk -F: '/^pub/{ print $5 }' | xargs -ri sh -c 'gpg --refresh {} ; sleep 5'

but since xargs(1) doesn't offer any speed controls itself, it requires an ugly roundabout via Shell.


Getting a random string

From the ChoosingPasswords page (see that page for an explanation)
tr -dc ' -~' < /dev/urandom | head -c 20

Getting a random number

There is a built in function called $RANDOM

echo $(($RANDOM % 100))

Extracting a stanza from a file

awk '/^iface bar/,/^$/ {print}'

This prints everything from the line starting with 'iface bar' to the next blank line to stdout.


Counting disk usage from find output

Thanks to #wlug, you can choose from a multitude of options

find /foo | xargs du -s
find /foo -print0 | xargs -0 du -sh
find /foo | xargs ls -l | awk '{sum += $5} END {print sum}'
find /foo -printf '%s\n' | awk '{sum+=$1} END {print sum}'
find /foo -printf '%k\n' | awk '{sum+=$1} END {print sum,"kbytes"}'

Find a random debian package that is not installed.

aptitude search ~g | while read ; do echo "$RANDOM $REPLY" ; done | sort -n | head -1 | cut -d' ' -f2-

AristotlePagaltzis


Rename a series of files using a regular expressions

There's a number of ways of doing this, all with various problems. I finally settled on the following (Thanks #wlug, and Isomer and Phil in particular)
find . -iname '*:*' -exec rename s/:/-/ '{}' ';'

This was needed in particular because the filenames had double spaces in them and trailing spaces, which ultimately defeated my rather rusty attempts at using bash. I'm replacing ':' with '-' because Macs allow you to create a file or a directory with a '/' in the name, but represent this on the filesystem as a ':'. This causes problems over CIFS shares however. -DanielLawson