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

All commands from sorted by
Terminal - All commands - 11,998 results
FILE=$(tempfile 2>/dev/null || echo .$RANDOM)
2011-08-31 10:39:11
User: s123iva
Functions: echo
Tags: dash notify
0

enter our information on the content file.

cat table-mv.txt | perl -pe 's{([^;]+);([^;]+)}{tbl$1/tbl$2}' | perl -pe 's{(\S+)}{perl -i -pe #s/$1/g# xxx.sql}' | tr "#" "\'" | bash
2011-10-05 15:55:34
User: hute37
Functions: cat perl tr
0

with a semicolon text file map, apply multiple replace to a single file

sudo echo "package url" >> /etc/apt/sources.list
2012-05-01 21:32:51
User: Gunni
Functions: echo sudo
0

">>" appends to the file

">" replaces the entire file

make sure to use ">>"

cat item_list | xargs -n1 -P<n> process_item
find . -name ".DS_Store" -print0 | xargs -0 rm -rf
unrar p -inul *.rar|mplayer -cache 100000 -
2013-09-19 18:45:01
User: B4ckBOne
0

Fire this up in the directory of the RAR achived files to watch any archived movie tv-show or other media without having to unpack it. I also added a cache/buffer to be able to seek back and forth.

tar --exclude-from=$excludefile -zcvp "$source" | openssl aes-128-cbc -salt -out $targetfile -k $key
2013-12-13 19:35:20
User: klausman
Functions: tar
0

Create compressed, encrypted backup from $source to $targetfile with password $key and exclude-file $excludefile

/usr/bin/lynx -dump -width 500 http://127.0.0.1/whm-server-status | grep GET | awk '{print $12}' | sort | uniq -c | sort -rn | head
2014-03-12 12:31:34
User: copocaneta
Functions: awk grep sort uniq
0

Easiest way to obtain the busiest website list (sorted by number of process running).

ssh -D 8080 -f -N srv1
2014-06-25 12:46:44
User: fr00tyl00p
Functions: ssh
0

Provides a SOCKS5 proxy on localhost port 8080 which will tunnel all connections through srv1 and run in the background.

<command> | curl -F 'clbin=<-' https://clbin.com
2014-10-21 13:02:18
User: colemar
0

Define alias for convenience:

alias clbin='curl -v -F "clbin=<-" https://clbin.com'

Paste man page:

man bash | clbin

Paste image:

curl -F 'clbin=@filename.jpg' https://clbin.com
perl -e 'use strict; use warnings; my $c; my $file = $ARGV[0]; open my $handle, "<", $file or die "$0: $file: $!\n"; while (<$handle>) { print $c++, " " x 5, $_; } close($handle);' <FILE>
2009-12-09 16:07:14
User: sputnick
Functions: perl
0

This is a joke for @putnamhill and @glaudiston

I'm pretty sure we can write longer if we want ;)

find . -iname '*.mp3' | while read song; do mpg321 ${song} -w - | oggenc -q 9 -o ${song%.mp3}.ogg -; done
2010-03-14 11:34:35
User: renich
Functions: find mpg321 read
Tags: ogg mpg321
0

This is not recommended... lossy -> lossy = lossier.

Still, you can do it! ;)

(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].

find . -iname pom.xml -type f -exec bash -c "cat {} | sed s/1\.0\.46\-SNAPSHOT/1\.0\.48\-SNAPSHOT/g > {}.tmp " \; -exec mv {}.tmp {} \;
2011-10-05 18:57:12
User: miguelbaldi
Functions: bash find mv
0

When a large maven release goes wrong, by deploying just some of the artifacts letting others behind, some projects got wrong SNAPSHOT versions. This command comes to help!

Tip: replace sed's regex by your version numbers

DISPLAY=":0.0" import -window root screenshot.png
2012-02-05 11:52:33
User: 0disse0
Tags: display
0

But if you use a screen saver, you must first disable your screen saver

kill $(ps ax | grep screensaver | grep -v grep | awk '{ print $1 }')

for a in $(cat sample.txt); do echo "${#a} $a";done|sort -n
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
cat *mscache* | awk -F '"' '{print $4":"$2}'
2013-01-06 06:54:57
User: mubix
Functions: awk cat
0

Convert Metasploit?s MSCACHE output to Hashcat version (performed in ~/.msf4/loot/):

grep -Po '^sub (\w+)' file.pm | cut -d' ' -f2
2013-03-07 12:31:34
Functions: cut grep
0

instead of printing the whole line, print just the capture matched, but with the "cut" pipe :( I'm so sad with grep.

emerge --moo
/usr/bin/lynx -dump -width 500 http://127.0.0.1/whm-server-status | awk 'BEGIN { FS = " " } ; { print $12 }' | sed '/^$/d' | sort | uniq -c | sort -n
ls /EMRCV5/
rsync --recursive --info=progress2 <src> <dst>
2014-10-21 22:19:44
User: koter84
Functions: rsync
Tags: rsync progress
0

update the output of rsync after completing a file but don't create newlines, just overwrite the last line, this looks a lot better in scripts where you do want to see a progress-indicator, but not the lengthy logs

this option is available since rsync 3.1.0

touch -acm yyyymmddhhMM.ss [file]
2009-02-05 21:07:09
User: wwest4
Functions: touch
0

...changes modify time and access time. also:

touch -r [file1] [file2] # make file2 timestamp match file1

man fetchmail | perl -ne 'undef $/; print $1 if m/^.*?(-k \| --keep.*)-K \| --nokeep.*$/smg'
2009-06-25 23:51:35
Functions: fetchmail man perl
0

Using perl, here, we grep the man page of fetchmail to find the paragraph starting with '-k | --keep' and ending before the paragraph starting with '-K | --nokeep'