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.

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

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

Tags

Hide

Functions

Commands using perl from sorted by
Terminal - Commands using perl - 334 results
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.

perl -e 'for(;;){@d=split("",`date +%H%M%S`);print"\r";for(0..5){printf"%.4b ",$d[$_]}sleep 1}'
2010-12-04 00:05:54
User: putnamhill
Functions: perl
9

Fun idea! This one adds seconds and keeps running on the same line. Perl's probably cheating though. :)

perl -pe 'eof()||s/\n/<SOMETEXT>/g' file.txt
2010-12-02 01:19:27
User: eightmillion
Functions: perl
3

This command turns a multi-line file into a single line joined with <SOMETEXT>. To skip blank lines, use:

perl -pe '(eof()||s/^\s*$//)||s/\n/<SOMETEXT>/g' file.txt
apt-cache search perl | grep module | awk '{print $1;}' | xargs sudo apt-get install -y
-2

I used this to mass install a lot of perl stuff. Threw it together because I was feeling *especially* lazy. The 'perl' and the 'module' can be replaced with whatever you like.

find . -type f -print0 | xargs -0 perl -pi.save -e 'tr/A-Z/a-z/'
2010-11-25 13:55:34
User: depesz
Functions: find perl xargs
Tags: perl find regex
1

In this way it doesn't have problems with filenames with spaces.

perl -e "tr/[A-Z]/[a-z]/;" -pi.save $(find . -type f)
pcharc(){ perl -e 'for (0..255) {$_ = chr($_); print if /['$1']/}' | cat -v; echo;}
2010-11-13 00:32:41
User: putnamhill
Functions: cat perl
Tags: perl
1

Today I needed a way to print various character classes to use as input for a program I was writing. Also a nice way to visualize character classes.

dpkg -L iptables | perl -lne 'print if -f && -x'
git status | perl -F'\s' -nale 'BEGIN { $a = 0 }; $a = 1 if $_ =~ /changed but not updated/i; print $F[-1] if ( $a && -f $F[-1] )'
2010-10-15 07:58:14
Functions: perl
Tags: status git perl
0

Parse the output of git status.

Once the line '# Changed but not updated:' has passed print every last part of the line if it exists on disk.