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 by h3xx from sorted by
Terminal - Commands by h3xx - 20 results
tcpdump -w - |pv -bert >/dev/null
2011-12-14 00:24:02
User: h3xx
Functions: tcpdump
7

Real gurus don't need fancy tools like iftop or jnettop.

aspell -a <<< '<WORDS>'
cp --sparse=always <SRC> <DST>
2011-09-07 08:02:50
User: h3xx
Functions: cp
4

This causes cp to detect and omit large blocks of nulls. Sparse files are useful for implying a lot of disk space without actually having to write it all out.

http://en.wikipedia.org/wiki/Sparse_file

You can use it in a pipe too:

dd if=/dev/zero bs=1M count=5 |cp --sparse=always /dev/stdin SPARSE_FILE
<COMMAND> |:
2011-08-28 23:48:29
User: h3xx
25

This is shorter and actually much faster than >/dev/null (see sample output for timings)

Plus, it looks like a disappointed face emoticon.

grep ^Dirty /proc/meminfo
2011-08-24 08:48:49
User: h3xx
Functions: grep
30

Ever ask yourself "How much data would be lost if I pressed the reset button?"

Scary, isn't it?

lsr() { find "${@:-.}" -print0 |sort -z |xargs -0 ls $LS_OPTIONS -dla; }
2011-08-15 03:10:58
User: h3xx
Functions: find ls sort xargs
2

Tells you everything you could ever want to know about all files and subdirectories. Great for package creators. Totally secure too.

On my Slackware box, this gets set upon login:

LS_OPTIONS='-F -b -T 0 --color=auto'

and

alias ls='/bin/ls $LS_OPTIONS'

which works great.

echo $(($(ulimit -u)-$(pgrep -u $USER|wc -l))
grep -2riP '\b(fuck|shit|bitch|tits|ass\b)' /usr/src/linux/
xargs -P 3 -n 1 <COMMAND> < <FILE_LIST>
2011-07-25 22:53:32
User: h3xx
Functions: xargs
0

For instance:

find . -type f -name '*.wav' -print0 |xargs -0 -P 3 -n 1 flac -V8

will encode all .wav files into FLAC in parallel.

Explanation of xargs flags:

-P [max-procs]: Max number of invocations to run at once. Set to 0 to run all at once [potentially dangerous re: excessive RAM usage].

-n [max-args]: Max number of arguments from the list to send to each invocation.

-0: Stdin is a null-terminated list.

I use xargs to build parallel-processing frameworks into my scripts like the one here: http://pastebin.com/1GvcifYa

(trap 'true' ERR; exec <SEGFAULT_PRONE_PROGRAM>)
2011-07-25 02:30:52
User: h3xx
Functions: exec trap
Tags: trap script error
0

Crash Override, man!

Apparently the exec call tricks BASH into setting the output buffer size to 0 under the assumption that the system (or the calling shell) will handle the output buffering. trapping the ERR signal will stop the subshell from dying and sending the ERR signal to the main script--which will terminate immediately if it does--when the program fails.

The only problem is that the kernel will output a whole bunch of stack trace garbage directly to the console device once the process segfaults, so there's no way to prevent it from being output [that I know of].

xxd -p <<< <STRING>
read VAR1 VAR2 VAR3 <<< aa bb cc; echo $VAR2
jpegtran -optimize -outfile temp.jpg <JPEG> && mv temp.jpg "$_"
2011-07-24 08:55:46
User: h3xx
Functions: mv
Tags: jpeg
0

You can also save EXIF information by copying it to temp.jpg:

jpegtran -optimize -outfile temp.jpg <JPEG> && jhead -te temp.jpg "$_" && mv temp.jpg "$_"
aumix -v +5
2011-07-24 07:41:40
User: h3xx
Functions: aumix
Tags: alsa
0

Or,

aumix -v -5

Map these to key combinations in your window manager and who needs special buttons?

xrandr -s 0
2011-07-24 07:38:01
User: h3xx
2

This forces X back to its maximum resolution configured. To get a list, type `xrandr'.

mplayer -af volnorm=2:0.75 dvd://
2011-07-24 07:26:51
User: h3xx
Tags: mplayer volume
2

Works really well for playing DVDs, which have the volume turned way down for some reason. The `2' method is better IMHO because it will adjust to changing loud/soft parts.

If you want to add it to your ~/.mplayer/config:

# format: volnorm[=method:target]

# method:

# 1: use single sample (default)

# 2: multiple samples

# target:

# default is 0.25

af-add=volnorm=2:0.75

gpg -e --default-recipient-self <SENSITIVE_FILE> && shred -zu "$_"
2011-07-24 05:51:47
User: h3xx
Functions: gpg shred
Tags: GPG shred
0

Make sure the file contents can't be retrieved if anyone gets ahold of your physical hard drive.

With hard drive partition:

gpg --default-recipient-self -o /path/to/encrypted_backup.gpg -e /dev/sdb1 && shred -z /dev/sdb1

WARNING/disclaimer: Be sure you... F&%k it--just don't try this.

mplayer -playlist <(find "$PWD" -type f)
2011-07-24 03:27:03
User: h3xx
Functions: find
Tags: mplayer fd
0

Skip forward and back using the < and > keys. Display the file title with I.

zgrep CONFIG_MAGIC_SYSRQ /proc/config.gz
export GREP_OPTIONS='--color=auto'
2011-07-24 01:32:10
User: h3xx
Functions: export
Tags: color grep
1

This will affect all invocations of grep, even when it is called from inside a script.