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.


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:



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.




All commands from sorted by
Terminal - All commands - 11,592 results
FOR /F "tokens=3* delims=[]=," %A IN ('SET ARRAY[') DO ( echo %A -- %B )
2010-08-10 12:12:27
User: Marco
Functions: echo

Loops over array of a system var, splits its values and puts the values into %A, %B, %C, %D, and so on.

Create array before, like

set ARRAY[0]=test1,100


set ARRAY[1]=test2,200

Be sure to replace %A, %B, etc. with %%A, %%B, etc. when using this from inside of batch files.

function crtonl { perl -i -ape 's/\r/\n/g;' $* ; }
2009-03-25 20:28:32
User: totoro
Functions: perl
Tags: files

Many Mac OS X programs, especially those in Microsoft:Office, create ASCII files with lines terminated by CRs (carriage returns). Most Unix programs expect lines separated by NLs (newlines). This little command makes it trivial to convert them.

/sbin/ifconfig eth0 | grep "inet addr" | sed -e 's/.*inet addr:\(.*\) B.*/\1/g'
ruby -e 'File.foreach("list") {|i| print `nslookup #{i}`}'
locate munin | xargs rm -r
htpdate -P proxy www.google.com www.yahoo.com www.commandlinefu.com
2009-02-13 17:31:11
User: piyo

HTP (HTTP Time Protocol) is an alternative way of getting "good enough" synchronized time. htpdate will give you near-second accuracy.

It works where NTP/SNTP does not because of firewalls and proxies. Of course, if NTP/SNTP can be used, use that instead.


htp is not in Ubuntu!

complete -W "$(while IFS=' ,' read host t; do echo $host; done < ~/.ssh/known_hosts)" ssh
2011-02-10 03:34:35
User: smax

Simple and fast variant, not using external programs. Another variation:

complete -W "$(while read line; do echo ${line%%[, ]*}; done < ~/.ssh/known_hosts)" ssh

HashKnownHosts must be off, of course.

pss() { ps -eo pid,args | sed '/'"$1"'/!d;/sed/d' ; }
2011-03-14 15:51:49
User: vando
Functions: ps sed

I know you can use pidof but with this you can know the specific PID with his command arguments (useful if you're running various proccess with same application)

command & echo $!
2011-06-08 18:16:38
User: Mahrud
Functions: command echo

Actually $! is an internal variable containing PID of the last job in background.

More info: http://tldp.org/LDP/abs/html/internalvariables.html#PIDVARREF

Using $! for job control:

possibly_hanging_job & { sleep ${TIMEOUT}; eval 'kill -9 $!' &> /dev/null; }
find . -user root | xargs sudo chown me:me
2012-04-24 18:29:13
Functions: chown find sudo xargs

be careful where you execute this from

do a 'sudo ls' beforehand to prime sudo to not ask for your password

lgrep() { /bin/ls -A --color=always ${2:-.} | /bin/grep $1 ; }
netstat -tunlp
2010-06-07 13:26:05
User: ender_x
Functions: netstat
Tags: netstat

Shows you all listening tcp/udp ports, and what program has them open(depending on rights)

seq -s^2+ 11 |rev| cut -d'+' -f2- | rev | bc
2011-02-10 08:41:14
User: rubenmoran
Functions: cut rev seq
Tags: seq sum math

I can't put the last ^2 with seq, so I reverse it to delete the last +N. So for doing sum(N^2) you have to do sum((N+1)^2). Must be a better way.

pgrep -fl
emerge -e system && emerge -e system && emerge -e world && emerge -e world
2009-02-18 13:51:31
User: enlightend

This is a command you see mentioned alot by Gentoo monkeys.

They say to use it after every update of GCC, any library you might use and glibc.

They argue that compiling and recompiling everything like this will optimize the system alot more because you are recompiling the entire system (gcc, glibc etc) with nativly compiled versions of themselves. Same goes for all libraries etc.

I doubt the difference in working speed is really worth the hours and hours you end up having your computer compile the same stuff again and again though.

ls \\someserver\c$\inetpub\wwwroot -r -i web.config | Select-String "SomeMachineName"
2009-03-25 22:36:58
User: cbilson
Functions: ls

Finds all files of a certain name and reports all line with the string. Very simple.

printf "%50s\n"|tr ' ' -
2010-01-07 08:49:46
User: rodolfoap
Functions: printf tr

Better -and faster- using bash printf.

watch -n 1 :
2009-03-25 23:00:28
User: penpen
Functions: watch
Tags: Linux unix

'watch' repeatedly (default every 2 seconds, -n 1 => every second) runs a command (here ':', a shorthand for 'true'), displays the output (here nothing) and the date and time of the last run.

I thought it to be obvious but it seemingly is not: to exit use Ctrl-C.

grep -c '^From ' mbox_file
for i in $(seq 1 20); do while read line; do echo "$i: $line"; done<$i.py; done
perl -m'AptPkg::Cache' -le '$c=AptPkg::Cache->new; for (keys %$c){ push @a, $_ if $c->{$_}->{'CurrentState'} eq 'Installed';} print for sort @a;'
2011-03-14 23:56:43
User: dbbolton
Functions: perl sort

A space-padded version:

perl -m'AptPkg::Cache' -e '$c=AptPkg::Cache->new; for (keys %$c){ push @a, $_ if $c->{$_}->{'CurrentState'} eq 'Installed';} print "$_ " for sort @a;'
YEST=`perl -w -e '@yest=localtime(time-86400);printf "%d%.2d%.2d",$yest[5]+1900,$yest[4]+1,$yest[3];'`
2009-05-19 08:54:06
User: sharfah
Tags: perl

Returns yesterday's date in the format yyyyMMdd

find . -type d -exec sh -c "normalize-audio -b \"{}\"/*.mp3" \;
2009-12-08 03:13:13
Functions: find sh

Execute this in the root of your music library and this recurses through the directories and normalizes each folder containing mp3s as a batch. This assumes those folders hold an album each. The command "normalize-audio" may go by "normalize" on some systems.

TOM=`perl -w -e '@tom=localtime(time+86400);printf "%d%.2d%.2d",$tom[5]+1900,$tom[4]+1,$tom[3];'`
2009-05-19 08:54:27
User: sharfah
Tags: perl

Returns tomorrow's date in the format yyyyMMdd

2010-09-11 18:51:41
User: mensa13

In case the line you want to join start with a char different than ", you may use \n.*"\n as regex.