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.




Commands tagged perl from sorted by
Terminal - Commands tagged perl - 171 results
ipconfig getpacket en0 | grep yi| sed s."yiaddr = "."en0: ". ipconfig getpacket en1 | grep yi| sed s."yiaddr = "."en1: ".
perl -e 'if(opendir D,"."){@a=readdir D;print $#a-1,"\n"}'
2009-07-23 20:14:33
User: recursiverse
Functions: perl
Tags: perl ls
time perl -e 'if(opendir D,"."){@a=readdir D;print $#a - 1,"\n"}'


real 0m0.497s

user 0m0.220s

sys 0m0.268s

time { ls |wc -l; }


real 0m3.776s

user 0m3.340s

sys 0m0.424s


** EDIT: turns out this perl liner is mostly masturbation. this is slightly faster:

find . -maxdepth 1 | wc -l

sh-3.2$ time { find . -maxdepth 1|wc -l; }


real 0m0.456s

user 0m0.116s

sys 0m0.328s

** EDIT: now a slightly faster perl version

perl -e 'if(opendir D,"."){++$c foreach readdir D}print $c-1,"\n"'

sh-3.2$ time perl -e 'if(opendir D,"."){++$c foreach readdir D}print $c-1,"\n"'


real 0m0.415s

user 0m0.176s

sys 0m0.232s

wget -q -O - 'URL/full?orderby=starttime&singleevents=true&start-min=2009-06-01&start-max=2009-07-31' | perl -lane '@m=$_=~m/<title type=.text.>(.+?)</g;@a=$_=~m/startTime=.(2009.+?)T/g;shift @m;for ($i=0;$i<@m;$i++){ print $m[$i].",".$a[$i];}';
2009-07-23 14:48:54
Functions: perl wget

substitute the URL with your private/public XML url from calendar sharing settings

substitute the dates YYYY-mm-dd

adjust the perl parsing part for your needs

ifconfig | perl -nle'/dr:(\S+)/ && print $1'
2009-07-23 09:33:31
User: xsawyerx
Functions: ifconfig perl

Fetches the IPs and ONLY the IPs from ifconfig. Simplest, shortest, cleanest.

Perl is too good to be true...

(P.S.: credit should go to Peteris Krumins at catonmat.net)

perl -e '$p=qr!(?:0|1\d{0,2}|2(?:[0-4]\d?|5[0-5]?|[6-9])?|[3-9]\d?)!;print((shift=~m/^$p\.$p\.$p\.$p$/)?1:0);'
2009-07-12 00:24:29
User: speaker
Functions: perl

This command will output 1 if the given argument is a valid ip address and 0 if it is not.

(curl -d q=grep http://www.commandlinefu.com/search/autocomplete) | egrep 'autocomplete|votes|destination' | perl -pi -e 's/a style="display:none" class="destination" href="//g;s/<[^>]*>//g;s/">$/\n\n/g;s/^ +//g;s/^\//http:\/\/commandlinefu.com\//g'
2009-07-08 22:10:49
User: isaacs
Functions: egrep perl

There's probably a more efficient way to do this rather than the relatively long perl program, but perl is my hammer, so text processing looks like a nail.

This is of course a lot to type all at once. You can make it better by putting this somewhere:

clf () { (curl -d "q=$@" http://www.commandlinefu.com/search/autocomplete 2>/dev/null) | egrep 'autocomplete|votes|destination' | perl -pi -e 's/<a style="display:none" class="destination" href="//g;s/<[^>]*>//g;s/">$/\n\n/g;s/^ +|\([0-9]+ votes,//g;s/^\//http:\/\/commandlinefu.com\//g'; }

Then, to look up any command, you can do this:

clf diff

This is similar to http://www.colivre.coop.br/Aurium/CLFUSearch except that it's just one line, so more in the spirit of CLF, in my opinion.

perl -e 'print "P1\n256 256\n", map {$_&($_>>8)?1:0} (0..0xffff)' | display
2009-07-08 17:50:23
User: dstahlke
Functions: perl

OK, not the most useful but a good way to impress friends. Requires the "display" command from ImageMagick.

perl -ne '$sum += $_ for grep { /\d+/ } split /[^\d\-\.]+/; print "$sum\n"'
2009-06-16 06:39:08
User: obscurite
Functions: grep perl split

Good for summing the numbers embedded in text - a food journal entry for example with calories listed per food where you want the total calories. Use this to monitor and keep a total on anything that ouputs numbers.

for k in `git branch|perl -pe s/^..//`;do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k|head -n 1`\\t$k;done|sort -r
2009-06-03 08:25:00
User: brunost
Functions: echo head perl sort

Print out list of all branches with last commit date to the branch, including relative time since commit and color coding.

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

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

wget -q -O- http://www.gutenberg.org/dirs/etext96/cprfd10.txt | sed '1,419d' | tr "\n" " " | tr " " "\n" | perl -lpe 's/\W//g;$_=lc($_)' | grep "^[a-z]" | awk 'length > 1' | sort | uniq -c | awk '{print $2"\t"$1}'
2009-05-04 16:00:39
User: alperyilmaz
Functions: awk grep perl sed sort tr uniq wget

This command might not be useful for most of us, I just wanted to share it to show power of command line.

Download simple text version of novel David Copperfield from Poject Gutenberg and then generate a single column of words after which occurences of each word is counted by sort | uniq -c combination.

This command removes numbers and single characters from count. I'm sure you can write a shorter version.

for i in `screen -ls | perl -ne'if(/^\s+\d+\.([^\s]+)/){print $1, " "}'`; do gnome-terminal -e "screen -x $i"; done
2009-04-25 22:39:24
User: hank
Functions: perl
Tags: screen Linux perl

There was another line that was dependent on having un-named screen sessions. This just wouldn't do. This one works no matter what the name is. A possible improvement would be removing the perl dependence, but that doesn't effect me.

ldapsearch -v -H ldap://<server> -x -D cn=<johndoe>,cn=<users>,dc=<ourdomain>,dc=<tld> -w<secret> -b ou=<lazystaff>,dc=<ourdomain>,dc=<tld> -s sub sAMAccountName=* '*' | perl -pne 's/(\d{11})\d{7}/"DATE-AD(".scalar(localtime($1-11644473600)).")"/e'
2009-04-22 00:57:34
User: flux
Functions: perl

When Ldapsearch queries an Active directory server, all the dates are shown using a timestamp of 18 digits. This perl regexp decodes them in a more human friendly notation. 11644473600 corresponds to some microsoft epoch.

$ perl -pi -e 's/\r\n/\n/g' <finelame>
2009-04-10 22:22:31
Functions: perl
Tags: perl dos2unix

Converts windows lined-style file to unix.

see http://en.wikipedia.org/wiki/Newline

Can be used to convert from linux2dos : just invert \r\n and \n.

perl -pi -e 's/foo/bar/g' $(grep -rl foo ./*)
2009-03-27 17:21:35
User: dopeman
Functions: grep perl
Tags: perl

This command will replace all instances of 'foo' with 'bar' in all files in the current working directory and any sub-directories.

du -hS / | perl -ne '(m/\d{3,}M\s+\S/ || m/G\s+\S/) && print'
2009-03-25 18:06:53
User: Alioth
Functions: du perl

Finds all directories containing more than 99MB of files, and prints them in human readable format. The directories sizes do not include their subdirectories, so it is very useful for finding any single directory with a lot of large files.

history | perl -lane '$lsize{$_} = scalar(@F); if($longest<$lsize{$_}) { $longest = $lsize{$_}; print "$_"; };' | tail -n1
function dpan () { PKG=`perl -e '$_=lc($ARGV[0]); s/::/-/g; print "lib$_-perl\n"' $1`; apt-get install $PKG; }
2009-03-12 15:39:38
User: dave0
Functions: install

Running 'cpan Module::Name' will install that module from CPAN. This is a simple way of using a similar command to install a packaged Perl module from a Debian archive using apt-get.

$ grep -rl oldstring . |xargs sed -i -e 's/oldstring/newstring/'
2009-03-03 20:10:19
User: netfortius
Functions: grep sed
Tags: perl sed

recursively traverse the directory structure from . down, look for string "oldstring" in all files, and replace it with "newstring", wherever found


grep -rl oldstring . |xargs perl -pi~ -e 's/oldstring/newstring'
:n,m w newfile.txt
2009-03-02 19:40:34
User: milmazz
Tags: vim perl

On command mode in Vim you can save parts of the current buffer in another file.

* The 'n' value represents the first line of the new file.

* The 'm' value represents the last line of the new file.

* newfile.txt is the newfile.

The results are similar to this command in perl:

perl -ne 'print if n..m' in.sql > out.sql