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.

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 - 344 results
perl -dwe 1
2011-05-05 20:28:03
User: bashrc
Functions: perl

Can also just use the debug mode like this.

perl -e 'rand($.) < 1 && ($line = $_) while <>;'
2011-04-25 21:28:26
Functions: perl
Tags: perl knuth

This is from perldoc -q random.*line, which says:

This has a significant advantage in space over reading the whole file in. You can find a proof of this method in The Art of Computer Programming, Volume 2, Section 3.4.2, by Donald E. Knuth.

Who am I to argue with Don Knuth?

files=$(find /dir/file -name *.txt -exec grep -l a {} \;) && perl -p -i -e 's/old/new/g;' $files
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

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

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

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

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

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

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

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

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

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

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

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

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