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 hex from sorted by
Terminal - Commands tagged hex - 23 results
echo -n text | hexdump -C
2014-02-21 09:15:15
User: Tomofumi
Functions: echo hexdump
Tags: sed hex ascii
0

hexdump could be used for conversion too!

echo -n 023135 | perl -pe 's/([0-9a-f]{2})/chr hex $1/gie' | nc -4u -q1 -p5001 192.168.0.100 2000
2013-09-18 14:31:47
User: sucotronic
Functions: echo perl
1

Use it to send raw data to a networked device. Used to interact with relay controller board whose documentation is lost, so use wireshark to sniff the sent data and replayed using the command.

echo 474e552773204e6f7420556e697821 | sed -e 's/\(.\{2\}\)/\\\\x\1/g' | xargs echo -e
python -c 'print "hello".encode("hex")'
2011-12-13 23:05:17
User: atoponce
Functions: python
2

You can use "decode()" in a similar manner:

python -c 'print "68656c6c6f".decode("hex")'
python -c 'print hex(1337)'
2011-12-13 22:03:10
User: atoponce
Functions: python
0

Python is always such much more readable than most shell scripting.

hex() { printf "%X\n" $1; }
hex() { echo $1 16op | dc; }
hex() { bc <<< "obase=16; $1"; }
2011-12-09 13:57:05
User: bjomape
Functions: bc
Tags: bash hex bc numbers
4

Use the standard calculator bc to convert decimals to hex

echo $((0x1fe)) $((033))
2011-12-06 02:00:42
User: Mozai
Functions: echo
Tags: bash hex numbers
16

Bash can accept '0x' and '0' notation for hexidecimal and octal numbers, so you just have to output the values.

myhex=$(printf '%02X' ${myip//./ };)
2011-11-30 15:12:28
Functions: printf
Tags: IP hex printf octet
2

Converts IP octets to hex using printf command. Useful for generating pxeboot aliases in the pxelinux.cfg folder.

xxd -p <<< <STRING>
echo "text" | hd
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.

echo -n 'text' | perl -pe 's/(.)/sprintf("\\x%x", ord($1))/eg'
2010-07-14 12:20:42
User: putnamhill
Functions: echo perl
Tags: perl hex ascii
1

Here's a version that uses perl. If you'd like a trailing newline:

perl -pe 's/(.)/sprintf("\\x%x", ord($1))/eg; END {print "\n"}'
echo -n 'text' | xxd -ps | sed 's/[[:xdigit:]]\{2\}/\\x&/g'
2010-07-13 21:46:30
User: camocrazed
Functions: echo sed
Tags: sed hex ascii
0

Same as another one I saw, just with a cleaner sed command

Edit: updated the sed command to use the [[:xdigit:]] character class - more portable between locales

Note that it will have a newline inserted after every 32 characters of input, due to the output of xxd

echo -n 'text' | xxd -ps | sed -e ':a' -e 's/\([0-9]\{2\}\|^\)\([0-9]\{2\}\)/\1\\x\2/;ta'
2010-06-09 12:20:02
User: sata
Functions: echo sed
Tags: hex ascii
1

Converts the ascii text to hex from bash.

Check the sample output.

echo $ascii | perl -ne 'printf "%x", ord for split //'
echo $ascii | perl -ne 'printf ("%x", ord($1)) while(/(.)/g); print "\n";'
echo $hex | perl -pe 's/(..)/chr(hex($1))/ge'
echo 'obase=16; C+F' | bc
2010-04-14 20:35:31
User: rkulla
Functions: echo
Tags: hex bc math asm
4

To do hex to binary: echo 'ibase=16; obase=2; 16*16' | bc # prints: 111100100

To do 16*16 from decimal to hex: echo 'ibase=10; obase=16; 16*16' | bc # prints: 100

You get the idea... Alternatively, run bc in interactive mode (see man page)

cat testfile | hexdump -C
echo -n $HEXBYTES | xxd -r -p | dd of=$FILE seek=$((0x$OFFSET)) bs=1 conv=notrunc
2009-03-11 17:02:24
User: zombiedeity
Functions: dd echo
2

Replace (as opposed to insert) hex opcodes, data, breakpoints, etc. without opening a hex editor.

HEXBYTES contains the hex you want to inject in ascii form (e.g. 31c0)

OFFSET is the hex offset (e.g. 49cf) into the binary FILE