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 - 167 results
svn log | perl -l40pe 's/^-+/\n/'
2011-10-14 16:02:22
User: bazzargh
Functions: perl
Tags: svn perl svn log

the output of svn log is annoying to grep, since it spreads the useful info over multiple lines. This compacts the output down to one line so eg you can grep for a comment and see the rev, date & committer straight away.

Updated: MUCH shorter, easier to remember. Now it just replaces newlines with spaces, except on '---' lines.

perl -i -pe 'BEGIN{undef $/;} s/START.*?STOP/replace_string/smg' file_to_change
tail -f LOGFILE | perl -ne '`say "$_"`;'
2011-09-16 05:33:22
User: tamouse
Functions: perl tail
Tags: perl tail say

say only processes a complete file, at eof, so following a file isn't possible. Quick and dirty perl oneliner to feed each line from the tail -f to say. Yes, expensive to lauch a new process each line.

This little ditty was prompted by a discussion on how horrible it is to use VoiceOver on ncurses programs such as irssi.

sudo find / -type f | perl -MFile::Basename -ne '$counts{dirname($_)}++; END { foreach $d (sort keys %counts) {printf("%d\t%s\n",$counts{$d},$d);} }'|sort -rn | tee /tmp/sortedfilecount.out | head
2011-09-14 19:41:19
User: tamouse
Functions: find perl sort sudo tee

Find which directories on your system contain a lot of files.

Edit: much shorter and betterer with -n switch.

cd /path/to/pmwiki/wiki.d;/bin/ls -1 | perl -ne 'my ($group,$name)=split(/\./);$counts{$group}++;' -e 'END { foreach $group (sort keys %counts) {printf("%d\t%s\n",$counts{$group},$group);} }'|sort -rn
2011-09-14 19:33:39
User: tamouse
Functions: cd perl sort
Tags: sort perl pmwiki

PmWiki stores wiki pages as Group.Name. Simply split the directory listing and count frequency of group occurances.

route -n | perl -ne '$ANY=""; /^$ANY/ and split /\s+/ and print "Gateway to the World: ",($_[1]!=$ANY)?$_[1]:(`ip address show $_[$#_]`=~/peer ([0-9\.]+)/ and $1),", via $_[$#_].\n"'
wget -q -O - http://www.perl.org/get.html | grep -m1 '\.tar\.gz' | sed 's/.*perl-//; s/\.tar\.gz.*//'
curl -s http://www.perl.org/get.html | grep -m1 '\.tar\.gz' | sed 's/.*perl-//; s/\.tar\.gz.*//'
find /var/spool/mqueue -type f -mtime +7 | perl -lne unlink
2011-08-19 15:22:02
User: mengesb
Functions: find perl

Find all files in /var/spool/mqueue older than 7 days, pass to perl to efficiently delete them (faster than xargs or -exec when you've got millions or hundreds of thousands to delete). Naturally the type, directory, and file age vars can be adjusted to meet your specific needs.

ack; pcregrep
2011-08-14 18:01:36
User: depesz
Tags: perl grep regex

Much better alternatives - grep-alikes using perl regexps. With more options, and nicer outputs.

prep () { perl -nle 'print if '"$1"';' $2 }
2011-08-13 14:29:26
User: dbbolton
Functions: perl
Tags: perl grep regex

If you've ever tried "grep -P" you know how terrible it is. Even the man page describes it as "highly experimental". This function will let you 'grep' pipes and files using Perl syntax for regular expressions.

The first argument is the pattern, e.g. '/foo/'. The second argument is a filename (optional).

cat subtitles.txt | perl -pe 's/} /}/g; s/{(\d+)}/=1=/; $f1=(24/25*$1); s/{(\d+)}/=2=/; $f2=(24/25*$1); $f1=~s/\..*//; $f2=~s/\..*//; s/=1=/{$f1}/; s/=2=/{$f2}/; ' > subtitles_newfps.txt
2011-07-30 15:34:43
User: ciekawy
Functions: cat perl

this command example converts to 25 fps subtitles that were originally created for 24 fps movie

lsof -nPi | txt2html > ~/lsof.html
2011-07-28 14:01:21
User: zlemini
Tags: perl cpan lsof

The output of lsof is piped to txt2html which converts it to html.

# Perl module HTML::TextToHTML needed

getent passwd $(whoami) | echo "$(perl -ne '/^([^:]+):[^:]+:[^:]+:[^:]+:([^ ]+) ?([^,]+)?,([^,]*),([^,]*),([^:,]*),?([^:,]*)/ and printf "MECARD:N:$3,$2;ADR:$5;TEL:$4;TEL:$6;EMAIL:$1@"')$HOSTNAME;;" | qrencode -o myqr.png
2011-07-25 12:59:57
User: Strawp
Functions: echo getent passwd
Tags: perl qr qrencode

If your contact information was entered when your user account was created (it gets added to /etc/passwd) then this gets that info and creates a QR code for you automatically

sort -R
2011-07-15 15:35:27
User: RyanM
Functions: sort

Randomizes a file. The opposite of sort is sort -R!

tail -f /var/log/squid/access.loc | ccze -CA
perl -mText::Highlight -E 'say Text::Highlight->new(ansi => 1)->highlight(Perl => do { local (@ARGV,$/) = shift; <> }) ' path/to/perl-file.pl
perl -ne 'print if m{\Q/*\E}x .. m{\Q*/\E}x or m{\/\/}x' *.c
2011-07-12 04:41:49
User: dbr
Functions: perl

more idiomatic version of the same, using the flip-flop-operator; also printing lines with '//'-style comments

perl -e 'my $in_comment = 0; while (<>) { $in_comment = 1 if m{\Q/*\E}; print if $in_comment; $in_comment = 0 if m{\Q*/\E}; }' *.cpp
2011-07-08 00:17:27
User: doherty
Functions: perl

This is a naive way of finding source code comments in source code files that use C-like comments: // and /*...*/

tail -f /var/log/squid/access.log | perl -p -e 's/^([0-9]*)/"[".localtime($1)."]"/e'
tail -f /var/log/logfile|perl -e 'while (<>) {$l++;if (time > $e) {$e=time;print "$l\n";$l=0}}'
2011-06-21 10:28:26
User: madsen
Functions: perl tail time
Tags: perl tail

Using tail to follow and standard perl to count and print the lps when lines are written to the logfile.

perl -MFile::Find -e"finddepth(sub{rmdir},'.')"
2011-05-23 08:45:34
User: igorfu
Functions: perl
Tags: perl delete

Recursively delete empty directories. Use with care.

rename 's/\d+/sprintf("%04d",$&)/e' *.jpg
2011-05-01 20:50:36
User: eightmillion
Functions: rename

This uses Perl's rename utility (you may have to call it as prename on your box) and won't choke on spaces or other characters in filenames. It will also zero pad a number even in filenames like "vacation-4.jpg".

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?

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 :-)