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 using od from sorted by
Terminal - Commands using od - 24 results
dd if=/dev/random count=1 bs=2 2>/dev/null | od -i | awk '{print $2}' | head -1
od -An -N1 -tu1 /dev/random
2013-09-10 08:57:16
User: sucotronic
Functions: od
6

It takes a byte from /dev/random whose source is the kernel entropy pool (better source than other solutions).

od -An -N6 -tx1 /dev/urandom | sed -e 's/^ *//' -e 's/ */:/g' -e 's/:$//' -e 's/^\(.\)[13579bdf]/\10/'
2012-08-31 13:15:03
User: outly3r
Functions: od sed
0

- The last sed expression ensures the unicast/multicast bit is set to zero

- The greedy space replacements are for portability across UNIX seds (note there's TWO spaces and not just one, as this web shows, in s/^ */)

cat /dev/urandom|od -t x1|awk 'NR > line { pos=int(rand()*15)+2;printf("%s",$pos);line=NR+(rand()*1000);digits = digits+2 } digits == 64 { print("\n");exit }'
2012-08-14 19:02:00
User: jetdillo
Functions: awk cat exit od
1

Use this the next time you need to come up with a reasonably random bitstring, like for a WPA/WPA2 PSK or something. Takes a continuous stream of bytes coming from /dev/urandom, runs it through od(1), picking a random field ($0 and $1 excluded) from a random line and then prints it.

SEED=$(head -1 /dev/urandom|od -N 1);for i in {1..10};do tmp=$(mkpasswd ${RANDOM});pass=${tmp:2:6};echo Pass $pass Crypt: $(mkpasswd $pass);done
comm -13 <(od -vw1 -tu1 dummy.txt|cut -c9-|sort -u) <(seq 0 127|sort)|perl -pe '$_=chr($_)'|od -c
2012-01-09 01:32:20
User: bazzargh
Functions: comm cut od perl seq sort
Tags: bash
0

Search in decimal rather than hex. od dumps the character list, cut to remove offsets, sort -u gives the used characters. seq gives the comparison list, but we need this sorted alphabetically for comm, which does the filtering. I drop to perl to convert back to characters (is there a better way?) and then use od to dump them in a print-safe format.

od -c <file>
2011-09-09 18:55:28
User: wu
Functions: od
Tags: binary hexdump
1

'od -c' works like 'hexdump -c' but is available on other operating systems that don't ship with hexdump (e.g. solaris).

2>/dev/null dd if=/dev/urandom bs=1 count=6 | od -t x1 |sed '2d;s/^0\+ //;s/ /:/g'
od /dev/urandom -w6 -tx1 -An|sed -e 's/ //' -e 's/ /:/g'|head -n 1
2011-05-16 15:05:34
User: karel1980
Functions: head od sed
1

Just increase the 1 at the end if you want to generate more than one.

(Alternative to "| head -n N" you could use the -b flag of od: -b $[6*N]

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)
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.

od -An -N10 -x /dev/random | md5sum | sed -r 's/^(.{10}).*$/\1/; s/([0-9a-f]{2})/\1:/g; s/:$//;'
echo -n "text" | od -A n -t x1 |sed 's/ /\\x/g'
2010-07-14 15:31:36
User: camocrazed
Functions: echo od sed
Tags: sed hex ascii od
4

If you're going to use od, here's how to suppress the labels at the beginning. Also, it doesn't output the \x, hence the sed command at the end. Remove it for space separated hex values instead

echo "text" | od -t x1
2010-07-14 14:53:25
User: max_allan
Functions: echo od
Tags: perl hex ascii
0

Just use "od" and it can also dump in decimal or octal.

(use -t x1 and not just -x or it confuses the byte order)

There is a load of other formatting options, I'm not sure if you can turn off the address at the start of the line.

od --format=x1 --address-radix=x mybinaryfile
2010-06-30 22:54:51
User: grinob
Functions: od
0

--format=x1 makes sure that 2 byte words do not get swapped to little-endian and that the output is exactly like the bytes are stored on disk.

--address-radix=x makes sure offsets are not output in default octal base but in hex.

--address-radix=n can also be used to only output data without offsets

od -An -w999 -t xC <<< "$1" | sed 's/[ ]\?\(c[23]\) \(..\)/%\1%\2/g;s/ /\\\\\x/g' | xargs echo -ne
2010-05-31 16:35:52
Functions: echo od sed xargs
1

It only encodes non-Basic-ASCII chars, as they are the only ones not well readed by UTF-8 and ISO-8859-1 (latin-1).

It converts all

* C3 X (some latin symbols like ASCII-extended ones)

and * C2 X (some punctuation symbols like inverted exclamation)

...UTF-8 double byte symbols to escaped form that every parser understands to form the URLs. I didn't encode spaces and the rest of basic punctuation, but supposedly, space and others are coded as \x20, for example, in UTF-8, latin-1 and Windows-cp1252.... so its read perfectly.

Please feel free to correct, the application to which I designe that function works as expected with my assumption.

Note: I specify a w=999, I didn't find a flag to put unlimited value.

I just suppose very improbable surpass the de-facto 255 (* 3 byte max) = 765 bytes length of URL

2>/dev/null dd if=/dev/urandom bs=1 count=6 | od -t x1|sed '2d;s/0000000 *//;s/ /:/g;s/::*$//'
2010-04-30 16:57:59
User: hckhckhck
Functions: dd od sed
0

Mac OS X needs some clean up. First command works in Linux, Solaris just needs the "0000000"'s removed

2>/dev/null dd if=/dev/urandom bs=1 count=6 | od -t x1 | sed '2d;s/^0\+ //;s/ /:/g'
od -c <FILE> | grep --color '\\.'
2009-12-11 02:15:48
User: sputnick
Functions: grep od
Tags: od
6

For fancier and cleaner output, try the following snippet :

showendlines(){ while read i; do od --address-radix=n --width=$(wc -c <<< "$i") -c <<< "$i" | perl -pe 's/.\K\s{2,3}//g'; done < $1 | grep --color '\\.'; }

Now you can run that with :

showendlines <FILE>

Thanks to prince_jammys to "debug" my English ;)

echo -n a | od -d | sed -n "s/^.* //gp"
2009-10-17 15:46:00
User: stf42
Functions: echo od sed
1

the shortest command for character 'a' i know

head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p'
od -vt x1 /tmp/spaghettifile
printf %d 0x`dd if=/dev/urandom bs=1 count=4 2>/dev/null | od -x | awk 'NR==1 {print $2$3}'`
2009-02-18 16:23:09
User: introp
Functions: awk od printf
1

Sometimes, in a shell script, you need a random number bigger than the range of $RANDOM. This will print a random number made of four hex values extracted from /dev/urandom.