Button to go to Home Page Home Button to go to About Page About Button to go to Archive Page Archive

Which commands can make words out of their options?

03/27/2017 • 4 minute read

Update 4/17: Cleaned up the code after some help from the gurus on codereview.stackexchange.com

I noticed that I always prefer using commands with options that spell out common words like ps -elf or ls -cat as they are easier to remember, so I decided to write a script to find out where other instances like this can happen:

#!/bin/bash

# Pull in a list of common Linux commands
commandList=(tar grep find ssh sed awk vim diff sort export xargs ls pwd cd gzip bzip2 unzip ftp crontab service ps free top df du cp mv cat mount chmod chown mkdir ifconfig uname whereis whatis locate man tail less su apt yum rpm ping date finger wget);

# Pipe successful "$command -$option" pairs to 'an' to generate anagrams
for command in ${commandList[@]} ; do
        (for option in {a..z} ; do
                if timeout -k 5 5 "$command" -$option &> /dev/null; then
                                printf $option
                fi
        done) | xargs an -w -d saneWordlist -m 3 2> /dev/null \
              | sed "s/^/ '$command' -/" >> commandOptions.log
done

Ugly, and not something I would ever put my name on professionally, but it gets the job done. It produces over 12,000 command -word combinations with the 50 sample input Linux commands. Here are some of my favorites it found:

ls -afro
ls -grinch
ls -algorithm
ls -akimbo
ls -albino
ls -angst
ls -badmouth
ls -calming
ls -flamingos
ls -frogman
ls -gambit
ls -groans
ls -gulps
ls -hacking
ls -hustling
ls -liar
ls -nachos
ls -obfuscating
ls -prodigal
ps -centaur
ps -gnu
ps -manure
ps -nuclear
free -tomb
free -bolt
df -milk
df -ham
mount -swirl
uname -savior
uname -prison
uname -pain
whereis -bums
whereis -bus

So I guess the lesson here is that ls can take pretty much any option.

How it works

It starts by pulling in a list of common Linux commands and storing them in commandList.

Then it iterates through the commandList and tries each command with every letter as an option. It has 5 seconds to run before SIGKILL is sent:

for command in $commandList ; do
    (for option in {a..z} ; do
        if timeout -k 5 5 "$command" -$option &> /dev/null 2>&1; then

If it exits successfully, loop through and output successful options without a new line character. Otherwise, do nothing:

    printf "$option"
fi

At the end of that, output successful options to an, a tool for generating anagrams. I allow only words over 3 characters and use a custom dictionary I made:

xargs an -w -d saneWordlist -m 3 2> /dev/null

Here is how I made my custom dictionary:

grep -v '[[:punct:]]' /usr/share/dict/words | sed  's/é/e/g' > saneWordlist

After an generates anagrams, let sed insert the command name at the start of the anagram list so we can identify who it belongs to and append to a file:

sed "s/^/ '$command' -/" >> commandOptions.log

As I said earlier, this is an ugly program. It is not made to exhaustively find each command option, but rather generate a good list to look through.

Areas for Improvement

man --where --all "$command" > /dev/null 2>&1
if (($? == 0)); then
    mapfile -t commandOptions < <(
    printf "$i-" && man $i \
    | sed -ne 's/.*\(-[A-Za-z],\).*/\1/p' \
    | sort -u \
    | tr -d ',-' \
    | tr -d '\n')

Email

You May Also Enjoy