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 -ne 'print "$1 " if /^Host (.+)$/' ~/.ssh/config
2011-08-21 14:51:20
User: bashrc
Functions: perl
4

I use this in my bashrc to expand hosts defined in ~/.ssh/config:

function _ssh_completion() {

perl -ne 'print "$1 " if /^Host (.+)$/' ~/.ssh/config

}

complete -W "$(_ssh_completion)" ssh

Here's a great article on how to setup your own ~/.ssh/config:

http://blogs.perl.org/users/smylers/2011/08/ssh-productivity-tips.html

find /var/spool/mqueue -type f -mtime +7 | perl -lne unlink
2011-08-19 15:22:02
User: mengesb
Functions: find perl
0

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.

strace -ff -e trace=file my_command 2>&1 | perl -ne 's/^[^"]+"(([^\\"]|\\[\\"nt])*)".*/$1/ && print'
2011-08-16 15:00:18
Functions: perl strace
16

Can be run as a script `ftrace` if my_command is substrituted with "$@"

It is useful when running a command that fails and you have the feeling it is accessing a file you are not aware of.

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

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
1

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

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
0

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

arp-scan -I eth0 -l | perl -ne '/((\d{1,3}\.){3}\d{1,3})/ and $ip=$1 and $_=`nmblookup -A $ip` and /([[:alnum:]-]+)\s+<00>[^<]+<ACTIVE>/m and printf "%15s %s\n",$ip,$1'
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
1

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
2

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},'.')"
perl -MFile::Find -e"finddepth(sub{rmdir},'.')"
2011-05-23 08:45:34
User: igorfu
Functions: perl
Tags: perl delete
1

Recursively delete empty directories. Use with care.

sudo aptitude markauto $(apt-cache showsrc PACKAGE | grep Build-Depends | perl -p -e 's/(?:[\[(].+?[\])]|Build-Depends:|,|\|)//g')
perl -dwe 1
2011-05-05 20:28:03
User: bashrc
Functions: perl
1

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
1

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