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.

Universal configuration monitoring and system of record for IT.
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

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!
Hide

Top Tags

Hide

Functions

Hide

Credits

Commands using perl from sorted by
Terminal - Commands using perl - 342 results
cat username_lovedtracks.xspf |perl -pe "s/.*<title>(.*)<\/title><creator>(.*)<\/creator>.*/Song: \1 Artist: \2/gi"> titles
2011-04-07 09:02:11
User: dustylc
Functions: cat perl
0

Change your exported xml love list from last.fm, into Song: songname Artist: artistname

perl -e 'unlink grep { -f -B } <*>'
2011-04-05 10:32:40
User: seungwon
Functions: grep perl
1

Please note that binary file checking is NOT perfect.

So, use it with caution.

It does not delete hidden files whose name has a leading '.' character.

And it regards an empty file as a binary file.

perl -le 'opendir DIR, "." or die; print while $_ = readdir DIR; closedir DIR'
2011-04-04 06:21:39
User: bierik
Functions: perl
1

Ever wanted to get the directory content with 'ls' or 'find' and had to wait minutes until something was printed? Perl to the rescue. The one-liner above(redirected to a file) took less than five seconds to run in a directory with more man 2 million files. One can adapt it to e.g. delete files that match a certain pattern.

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
-2

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;'
perl -ple 'BEGIN { $\ = "\r\n" }'
2011-03-01 09:45:37
Functions: perl
Tags: perl newline
0

Let -p do it's voodoo and do absolutely nothing but set the output record separator :-)

curl -Ls "http://support.dell.com/support/DPP/Index.aspx?c=us&cs=08W&l=en&s=biz&ServiceTag=$(dmidecode -s system-serial-number)"|egrep -i '>Your Warranty<|>Product Support for'|html2text -style pretty|egrep -v 'Request|View'|perl -pane 's/^(\s+|\})//g;'
2011-02-18 22:29:05
User: din7
Functions: egrep perl
-2

The dates in the output are Start Date, End Date, Days Remaining in warranty, respectively. This will only work if you are running it on a dell machine. You can substitute the dmidecode command with a service tag if you are not using a dell. Also, you have to either allow your user to run sudo dmidecode with no password or run this command as root.

perl -ne 'print if !$a{$_}++'
2011-02-17 02:18:44
User: doherty
Functions: perl
3

Reads stdin, and outputs each line only once - without sorting ahead of time. This does use more memory than your system's sort utility.

(echo foobar; echo farboo) | perl -E 'say[sort<>=~/./g]~~[sort<>=~/./g]?"anagram":"not anagram"'
2011-02-17 02:15:46
User: doherty
Functions: echo perl
2

This works by reading in two lines of input, turning each into a list of one-character matches that are sorted and compared.

perl -MText::Highlight -E '$h=Text::Highlight->new(ansi=>1); my $text=do{local $/; open my $fh, "<", $ARGV[0]; <$fh>}; say $h->highlight("Perl", $text);' path/to/perl-file.pl
2011-01-31 05:52:43
User: doherty
Functions: perl
1

This uses Text::Highlight to output the specified Perl file with syntax highlighting. A better alternative is my App::perlhl - find it on the CPAN: http://p3rl.org/App::perlhl

while true; do curl -s http://whatthecommit.com | perl -p0e '($_)=m{<p>(.+?)</p>}s' | cowsay; sleep 2; done
perl -nle 'printf "%0*v8b\n"," ",$_;'
perl -e 'printf "%vd\n",pack "N",rand 256**4'
perl -le '$,=".";print map int rand 256,1..4'
perl -le '$,=".";print map int rand 256,1..4'
perl -e 'printf join(".", ("%d")x4 ), map {rand 256} 1..4;'
perl -e 'printf join ".", map int rand 256, 1 .. 4;'
perl -e 'printf qq{%d\n}, time/86400;'
2011-01-14 21:56:19
User: gwchamb
Functions: perl
1

There are some environments that use this value for password and account expiration. It's helpful to be able to quickly determine the number of days since the Unix epoch (dse) when working directly with the configuration files as an administrator.

perl -le 'print time()'
perl -e 'print for(map{chr(hex)}("4861707079204E6577205965617221"=~/(.{2})/g)),"\n";'
cat /dev/urandom | hexdump -C | highlight ca fe 3d 42 e1 b3 ae f8 | perl -MTime::HiRes -pne "Time::HiRes::usleep(rand()*1000000)"
2010-12-29 21:26:18
User: doherty
Functions: cat hexdump perl
1

Nobody wants the boss to notice when you're slacking off. This will fill your shell with random data, parts of it highlighted. Note that 'highlight' is the Perl module App::highlight, not "a universal sourcecode to formatted text converter." You'll also need Term::ANSIColor.

perl -wlne'/title>([^<]+)/i&&rename$ARGV,"$1.html"' *.html
2010-12-29 05:39:41
User: mhs
Functions: perl
Tags: rename
9

The above one-liner could be run against all HTML files in a directory. It renames the HTML files based on the text contained in their title tag. This helped me in a situation where I had a directory containing thousands of HTML documents with meaningless filenames.

perl -ane 'print unless $x{$F[0]}++' infile > outfile
netstat -atn | perl -ane 'if ( $F[3] =~ /(\d+)$/ ) { $x{$1}=1 } END{ print( (grep {!$x{$_}} 32768..61000)[0] . "\n" )}'
netstat -atn | perl -0777 -ne '@ports = /tcp.*?\:(\d+)\s+/imsg ; for $port (32768..61000) {if(!grep(/^$port$/, @ports)) { print $port; last } }'
2010-12-14 20:44:37
User: bashrc
Functions: last netstat perl
1

Not really better - just different ;)

There's probably a really simple solution out there somewhere...

perl -MMIME::Base64 -ne 'print decode_base64($_)' < file.txt > out
2010-12-13 23:35:20
User: dmmst19
Functions: perl
2

If you are in an environment where you don't have the base64 executable or MIME tools available, this can be very handy for salvaging email attachments when the headers are mangled but the encoded document itself is intact.