Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands tagged random from sorted by
Terminal - Commands tagged random - 75 results
curl -s "https://www.random.org/cgi-bin/randbyte?nbytes=4" | od -DAn
2010-11-09 18:26:24
Functions: od
Tags: random
-1

Grab 4 bytes from www.random.org over ssl and format them as an integer

echo $(openssl rand 4 | od -DAn)
echo $RANDOM
2010-11-09 09:57:44
User: depesz
Functions: echo
Tags: random
7

works at least in bash. returns integer in range 0-32767. range is not as good, but for lots of cases it's good enough.

od -N 4 -t uL -An /dev/random | tr -d " "
2010-11-09 07:57:16
User: hfs
Functions: od tr
Tags: random
2

Reads 4 bytes from the random device and formats them as unsigned integer between 0 and 2^32-1.

dd if=/dev/urandom of=/dev/dsp
2010-10-27 09:25:02
User: khashmeshab
Functions: dd
-5

Sends random sounds to your sound card output (e.g. your speaker). Think... You can also run it remotely on another computer using SSH and scare its user!

dd if=/dev/urandom of=/dev/speaker bs=1
2010-10-27 09:22:40
User: khashmeshab
Functions: dd
-5

Sends random beeps to your PC-speaker. Think... You can also run it remotely on another computer using SSH and scare its user! Don't forget to run it on your dedicated hosting server and watch sysadmin's action from data-center's live remote cameras!

cycle(){ while :;do((i++));echo -n "${3:$(($i%${#3})):1}";sleep .$(($RANDOM%$2+$1));done;}
2010-10-08 23:45:40
User: putnamhill
Functions: echo sleep
Tags: sleep random
1

Cycles continuously through a string printing each character with a random delay less than 1 second. First parameter is min, 2nd is max. Example: 1 3 means sleep random .1 to .3. Experiment with different values. The 3rd parameter is the string. The sleep will help with battery life/power consumption.

cycle 1 3 $(openssl rand 100 | xxd -p)

Fans of "The Shining" might get a kick out of this:

cycle 1 4 ' All work and no play makes Jack a dull boy.'
sort -R SOMEFILE
2010-09-16 22:29:27
User: miniker84
Functions: sort
4

Works in sort (GNU coreutils) 7.4, don't know when it was implemented but sometime the last 6 years.

shuf SOMEFILE
feh --bg-center `ls | shuf -n 1`
man $(ls /bin | sed -n $((RANDOM % $(ls /bin | wc -l) + 1))p)
2010-08-20 17:15:33
User: putnamhill
Functions: ls man sed wc
Tags: man sed ls wc random
-2

Great idea camocrazed. Another twist would be to display a different man page based on the day of the year. The following will continuously cycle through all man pages:

man $(ls /bin | sed -n $(($(date +%j) % $(ls /bin | wc -l)))p)
curl -sL 'www.commandlinefu.com/commands/random' | awk -F'</?[^>]+>' '/"command"/{print $2}'
2010-08-13 11:42:42
User: putnamhill
Functions: awk
Tags: awk curl random
0

Splitting on tags in awk is a handy way to parse html.

wget -qO - www.commandlinefu.com/commands/random | grep "<div class=\"command\">" | sed 's/<[^>]*>//g; s/^[ \t]*//; s/&quot;/"/g; s/&lt;/</g; s/&gt;/>/g; s/&amp;/\&/g'
2010-08-12 23:58:24
User: smop
Functions: grep sed wget
Tags: random
1

retrieves the html from a random command line fu page, then finds commands on the page and prints them

alternatively, pipe to bash (add "| bash" to the end) to execute the command (very risky)

edit: had to adjust to properly display the portion that replaces HTML characters (e.g. &quot; -> ")

echo "$(od -An -N4 -tu4 /dev/urandom) % 5 + 1" | bc
awk 'BEGIN { srand(); print rand() }'
2010-08-07 14:33:12
User: dennisw
Functions: awk
Tags: random number
-2

2d6 dice:

awk 'BEGIN { srand(); a=int(rand()*6)+1; b=int(rand()*6)+1; print a " + " b " = " a+b }'

3 + 6 = 9

echo $[RANDOM%X+1]
2010-08-07 02:43:46
Functions: echo
24

If X is 5, it will about a number between 1 and 5 inclusive.

This works in bash and zsh.

If you want between 0 and 4, remove the +1.

fortune | cowsay -f $(ls /usr/share/cowsay/cows/ | shuf -n1)
2010-07-08 02:57:52
User: zed
Functions: ls
7

You need to have fortune and cowsay installed. It uses a subshell to list cow files in you cow directory (this folder is default for debian based systems, others might use another folder).

you can add it to your .bashrc file to have it great you with something interesting every time you start a new session.

perl -MDigest::SHA -e 'print substr( Digest::SHA::sha256_base64( time() ), 0, $ARGV[0] ) . "\n"' <length>
2010-04-30 21:45:46
User: udog
Functions: perl
1

Of course you will have to install Digest::SHA and perl before this will work :)

Maximum length is 43 for SHA256. If you need more, use SHA512 or the hexadecimal form: sha256_hex()

head -c10 <(echo $RANDOM$RANDOM$RANDOM)
2009-10-09 15:09:02
User: jgc
Functions: echo head
Tags: HEAD random
0

Makes use of $RANDOM environment variable.

echo "Decode this"| tr [a-zA-Z] $(echo {a..z} {A..Z}|grep -o .|sort -R|tr -d "\n ")
jot -s '' -r -n 8 0 9
2009-08-24 13:35:20
User: Hal_Pomeranz
Tags: random jot rs
1

Don't need to pipe the output into rs if you just tell jot to use a null separator character.

jot -r -n 8 0 9 | rs -g 0
echo $(( $RANDOM % 10 + 1 ))
randpw(){ < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-16};echo;}
2009-08-07 07:30:57
User: frozenfire
Functions: head tr
3

Generates password consisting of alphanumeric characters, defaults to 16 characters unless argument given.

awk 'BEGIN{srand()}{print rand(),$0}' SOMEFILE | sort -n | cut -d ' ' -f2-
2009-05-29 01:20:50
User: axelabs
Functions: awk cut sort
Tags: sort awk random
4

This appends a random number as a first filed of all lines in SOMEFILE then sorts by the first column and finally cuts of the random numbers.