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.

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.

UpGuard checks and validates configurations for every major OS, network device, and cloud provider.

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:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

Top Tags



Commands using perl from sorted by
Terminal - Commands using perl - 345 results
perl -i -pe 's/\r/\n/g' file
history | perl -F"\||<\(|;|\`|\\$\(" -alne 'foreach (@F) { print $1 if /\b((?!do)[a-z]+)\b/i }' | sort | uniq -c | sort -nr | head
2010-04-08 13:46:09
User: alperyilmaz
Functions: perl sort uniq

Most of the "most used commands" approaches does not consider pipes and other complexities.

This approach considers pipes, process substitution by backticks or $() and multiple commands separated by ;

Perl regular expression breaks up each line using | or < ( or ; or ` or $( and picks the first word (excluding "do" in case of for loops)

note: if you are using lots of perl one-liners, the perl commands will be counted as well in this approach, since semicolon is used as a separator

find ./ -iname "*.djvu" -execdir perl -e [email protected]=`djvutxt \"$ARGV[0]\"\|grep -c Berlekamp`; chomp @s; print $s[0]; print " $ARGV[0]\n"' '{}' \;|sort -n
2010-04-07 11:15:26
Functions: find grep perl sort

Count the occurences of the word 'Berlekamp' in the DJVU files that are in the current directory, printing file names from the one having the least to the most occurences.

perl -MStatistics::Descriptive -alne 'my $stat = Statistics::Descriptive::Full->new; $stat->add_data(@F[1..4]); print $stat->variance' filename
2010-04-02 21:16:12
User: alperyilmaz
Functions: perl

In this example, file contains five columns where first column is text. Variance is calculated for columns 2 - 5 by using perl module Statistics::Descriptive. There are many more statistical functions available in the module.

perl -MIO::All -e 'io(":8080")->fork->accept->(sub { $_[0] < io(-x $1 ? "./$1 |" : $1) if /^GET \/(.*) / })'
2010-03-31 15:03:55
User: Neo23x0
Functions: perl

First we accept a socket and fork the server. Then we overload the new socket as a code ref. This code ref takes one argument, another code ref, which is used as a callback.

The callback is called once for every line read on the socket. The line is put into $_ and the socket itself is passed in to the callback.

Our callback is scanning the line in $_ for an HTTP GET request. If one is found it parses the file name into $1. Then we use $1 to create an new IO::All file object... with a twist. If the file is executable("-x"), then we create a piped command as our IO::All object. This somewhat approximates CGI support.

Whatever the resulting object is, we direct the contents back at our socket which is in $_[0].

perl -e '$now=time; system "chsec -f /etc/security/passwd -s aixuser -a \"lastupdate=$now\""'
perl -pi -e 's/\r\n?/\n/g'
2010-03-18 17:48:16
User: putnamhill
Functions: perl
Tags: perl

This method will also convert mac line endings.

perl -ne 'BEGIN{undef $/}; print "$ARGV\t$.\t$1\n" if m/(first line.*\n.*second line)/mg'
2010-03-18 15:46:10
User: hfs
Functions: perl
Tags: perl grep

Using perl you can search for patterns spanning several lines, a thing that grep can't do. Append the list of files to above command or pipe a file through it, just as with regular grep. If you add the 's' modifier to the regex, the dot '.' also matches line endings, useful if you don't known how many lines you need are between parts of your pattern. Change '*' to '*?' to make it greedy, that is match only as few characters as possible.

See also http://www.commandlinefu.com/commands/view/1764/display-a-block-of-text-with-awk to do a similar thing with awk.

Edit: The undef has to be put in a begin-block, or a match in the first line would not be found.

echo sortmeplease | perl -pe 'chomp; $_ = join "", sort split //'
find -name ".php" -exec perl -pi -e 's/search/replace/g/' {} \;
username=bartonski;curl -s http://www.commandlinefu.com/commands/by/$username/json|perl -e 'BEGIN{$s=0;$n=0};END{print "Score: $s\nEntries: $n\nMean: ";printf "%3.2f\n",$s/$n}' -0173 -nae 'foreach $f (@F){if($f =~ /"votes":"(-*\d+)"/){$s += $1; $n++;}}'
2010-02-16 01:03:29
User: bartonski
Functions: perl

Like command #4845, prints score, number of entries, and average score.

echo "$url" | perl -MURI::Escape -ne 'chomp;print uri_escape($_),"\n"'
2010-02-13 00:44:48
User: eightmillion
Functions: echo perl
Tags: perl

Converts reserved characters in a URI to their percent encoded counterparts.

Alternate python version:

echo "$url" | python -c 'import sys,urllib;print urllib.quote(sys.stdin.read().strip())'
weather(){ curl -s "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=${@:-<YOURZIPORLOCATION>}"|perl -ne '/<title>([^<]+)/&&printf "%s: ",$1;/<fcttext>([^<]+)/&&print $1,"\n"';}
2010-02-10 01:23:39
User: eightmillion
Functions: perl

This shell function grabs the weather forecast for the next 24 to 48 hours from weatherunderground.com. Replace <YOURZIPORLOCATION> with your zip code or your "city, state" or "city, country", then calling the function without any arguments returns the weather for that location. Calling the function with a zip code or place name as an argument returns the weather for that location instead of your default.

To add a bit of color formatting to the output, use the following instead:

weather(){ curl -s "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=${@:-<YOURZIPORLOCATION>}"|perl -ne '/<title>([^<]+)/&&printf "\x1B[0;34m%s\x1B[0m: ",$1;/<fcttext>([^<]+)/&&print $1,"\n"';}

Requires: perl, curl

perl -e 'while(1){print"> ";eval<>}'
2010-02-03 21:48:33
User: kzh
Functions: perl

My Programming Languages professor assigned my class a homework assignment where we had to write a Perl interpreter using Perl. I really like Python's interactive command line interpreter which inspired this Perl script.

$ xrandr -q|perl -F'\s|,' -lane "/^Sc/&&print join '',@F[8..10]"
export QQ=$(mktemp -d);(cd $QQ; curl -s -O http://www.commandlinefu.com/commands/browse/sort-by-votes/plaintext/[0-2400:25];for i in $(perl -ne 'print "$1\n" if( /^(\w+\(\))/ )' *|sort -u);do grep -h -m1 -B1 $i *; done)|grep -v '^--' > clf.sh;rm -r $QQ
2010-01-30 19:47:42
User: bartonski
Functions: cd export grep mktemp perl sort

Each shell function has its own summary line, as a comment. If there are multiple shell functions with the same name, the function with the highest number of votes is put into the file.

Note: added 'grep -v' to the end of the pipeline, to eliminate extraneous lines containing only '--'. Thanks to matthewbauer for pointing this out.

define(){ local y="$@";curl -sA"Opera" "http://www.google.com/search?q=define:${y// /+}"|grep -Po '(?<=<li>)[^<]+'|nl|perl -MHTML::Entities -pe 'decode_entities($_)' 2>/dev/null;}
2010-01-29 05:01:11
User: eightmillion
Functions: grep perl

This function takes a word or a phrase as arguments and then fetches definitions using Google's "define" syntax. The "nl" and perl portion isn't strictly necessary. It just makes the output a bit more readable, but this also works:

define(){ local y="$@";curl -sA"Opera" "http://www.google.com/search?q=define:${y// /+}"|grep -Po '(?<=<li>)[^<]+';}

If your version of grep doesn't have perl compatible regex support, then you can use this version:

define(){ local y="$@";curl -sA"Opera" "http://www.google.com/search?q=define:${y// /+}"|grep -Eo '<li>[^<]+'|sed 's/<li>//g'|nl|perl -MHTML::Entities -pe 'decode_entities($_)' 2>/dev/null;}
mgc() { grep --exclude=cscope* --color=always -rni $1 . |perl -pi -e 's/:/ +/' |perl -pi -e 's/^(.+)$/vi $1/g' |perl -pi -e 's/:/ /'; }
2010-01-26 17:00:01
Functions: grep perl

This is a big time saver for me. I often grep source code and need to edit the findings. A single highlight of the mouse and middle mouse click (in gnome terminal) and I'm editing the exact line I just found. The color highlighting helps interpret the data.

perl -pe's/([\d.]+)/localtime $1/e;'
2010-01-19 18:47:58
User: tuxtutorials
Functions: perl

Converts Unix epoch time to localtime. Useful for any logs that only display epoch time.

perl -i~ -0777pe's/^/\!\#\/usr\/bin\/ksh\n/' testing
perl -le 'print $!+0, "\t", $!++ for 0..127'
grepp() { [ $# -eq 1 ] && perl -00ne "print if /$1/i" || perl -00ne "print if /$1/i" < "$2";}
2010-01-12 04:30:15
User: eightmillion
Functions: perl

This is a command that I find myself using all the time. It works like regular grep, but returns the paragraph containing the search pattern instead of just the line. It operates on files or standard input.

grepp <PATTERN> <FILE>


perl -le'print"-"x50'
2010-01-06 17:06:07
User: sputnick
Functions: perl
Tags: jot

Perl is installed by default on most modern OS when jot is not.

sfdisk /dev/sdb <(sfdisk -d /dev/sda| perl -pi -e 's/sda/sdb/g')
2009-12-22 22:45:41
Functions: perl

*as long as the drives are exactly the same* then this command copies the partition table on /dev/sda to /dev/sdb

<command> | perl -pe '/<regex/ && exit;'
2009-12-22 15:05:49
User: intuited
Functions: perl

Doesn't display the matching line. If you want that behaviour, you need to add "print && " before the 'exit'.