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 - 328 results
perl -pe's/([\d.]+)/localtime $1/e;'
2010-01-19 18:47:58
User: tuxtutorials
Functions: perl
3

Converts Unix epoch time to localtime. Useful for any logs that only display epoch time.

perl -i~ -0777pe's/^/\!\#\/usr\/bin\/ksh\n/' testing
perl -le 'print $!+0, "\t", $!++ for 0..127'
grepp() { [ $# -eq 1 ] && perl -00ne "print if /$1/i" || perl -00ne "print if /$1/i" < "$2";}
2010-01-12 04:30:15
User: eightmillion
Functions: perl
13

This is a command that I find myself using all the time. It works like regular grep, but returns the paragraph containing the search pattern instead of just the line. It operates on files or standard input.

grepp <PATTERN> <FILE>

or

<SOMECOMMAND> | grepp <PATTERN>
perl -le'print"-"x50'
2010-01-06 17:06:07
User: sputnick
Functions: perl
Tags: jot
4

Perl is installed by default on most modern OS when jot is not.

sfdisk /dev/sdb <(sfdisk -d /dev/sda| perl -pi -e 's/sda/sdb/g')
2009-12-22 22:45:41
Functions: perl
-3

*as long as the drives are exactly the same* then this command copies the partition table on /dev/sda to /dev/sdb

<command> | perl -pe '/<regex/ && exit;'
2009-12-22 15:05:49
User: intuited
Functions: perl
0

Doesn't display the matching line. If you want that behaviour, you need to add "print && " before the 'exit'.

<your command here> | perl -n -e 'print "$_" if 1 ... /<regex>/;'
2009-12-22 14:06:41
User: SuperFly
Functions: command perl
0

This command line will display the output of , from the first line of output, until the first time it sees a pattern matching .

You could specify the regex pattern /^$/ to look for the first blank line,

or /^foobar/ to look for the first line that starts with the word foobar.

perl -wl -e '@f=<>; for $i (0 .. $#f) { $r=int rand ($i+1); @f[$i, $r]=@f[$r,$i] if ($i!=$r); } chomp @f; print join $/, @f;' try.txt
perl -e 'for(@ARGV){s/x/*/g;s/v/sqrt /g;s/\^/**/g};print eval(join("",@ARGV)),$/;'
2009-12-21 21:03:27
User: JohnGH
Functions: perl
2

Once I wrote a command line calculator program in C, then I found this... and added to it a bit.

For ease of use I normally use this in a tiny Perl program (which I call pc for 'Perl Calculator')

#!/usr/bin/perl -w

die "Usage: $0 MATHS\n" unless(@ARGV);for(@ARGV){s/x/*/g;s/v/sqrt /g;s/\^/**/g};

print eval(join('',@ARGV)),$/;

It handles square roots, power, modulus:

pc 1+2 (1 plus 2)

3

pc 3x4 (3 times 4)

12

pc 5^6 (5 to the power of 6)

15625

pc v 49 ( square root of 49 )

7

pc 12/3 (12 divided by 3)

4

pc 19%4 (19 modulus 4)

3

(you can string maths together too)

pc 10 x 10 x 10

1000

pc 10 + 10 + 10 / 2

25

pc 7 x v49

49

perl -lne 'print for /url":"\K[^"]+/g' $(ls -t ~/.mozilla/firefox/*/sessionstore.js | sed q)
2009-12-14 00:51:54
User: sputnick
Functions: ls perl sed
0

If you want all the URLs from all the sessions, you can use :

perl -lne 'print for /url":"\K[^"]+/g' ~/.mozilla/firefox/*/sessionstore.js

Thanks to tybalt89 ( idea of the "for" statement ).

For perl purists, there's JSON and File::Slurp modules, buts that's not installed by default.

x=IO::Interface::Simple; perl -e 'use '$x';' &>/dev/null || cpan -i "$x"; perl -e 'use '$x'; my $ip='$x'->new($ARGV[0]); print $ip->address,$/;' <INTERFACE>
2009-12-13 02:23:40
User: sputnick
Functions: perl
1

Thanks to comment if that works or not...

If you have already typed that snippet or you know you already have IO::Interface::Simple perl module, you can type only the last command :

perl -e 'use IO::Interface::Simple; my $ip=IO::Interface::Simple->new($ARGV[0]); print $ip->address,$/;' <INTERFACE>

( The first perl command will install the module if it's not there already... )

perl -e 'use strict; use warnings; my $c; my $file = $ARGV[0]; open my $handle, "<", $file or die "$0: $file: $!\n"; while (<$handle>) { print $c++, " " x 5, $_; } close($handle);' <FILE>
2009-12-09 16:07:14
User: sputnick
Functions: perl
0

This is a joke for @putnamhill and @glaudiston

I'm pretty sure we can write longer if we want ;)

perl -pe 'print "$. "' <file>
perl -ne 'print "$. - $_"' infile.txt
2009-12-08 15:27:39
User: netp
Functions: perl
-1

This command prints all lines of a file together with is line number.

perl -e '$_=`ifconfig eth0`;/\d+.\d+.\d+.\d+ /; print $&,"\n";'
2009-12-05 14:24:48
Functions: perl
0

If you are interested in interfaces other than eth0 you will need to change eth0 to your interface name.

You could use this mammoth to nab the ip4 addresses of all your interfaces

perl -e '@_=`ifconfig -a`; sort(@_); foreach(@_) { /(inet addr\:)(\d+.\d+.\d+.\d+ )/; $_=$2; @uniq=grep($_ ne $prev && (($prev) = $_), @_);} print join "\n",@uniq,"\n"; '

it seems silly to have all this code when the following will work fine

ifconfig -a | grep "inet " | awk -F":" ' { print $2 } ' | cut -d " " -f1

perl -lne 'use POSIX; print strftime("%Y-%m-%d", localtime(time() - 86400));'
perl -e 'use Date::Calc qw(Today Week_Number); $weekn = Week_Number(Today); print "$weekn\n"'
find . -type f | perl -lne 'print if -T;' | xargs egrep "somepattern"
find . -type l | perl -lne 'print if ! -e'
perl -pe 's/%([0-9a-f]{2})/sprintf("%s", pack("H2",$1))/eig'
perl -p -i -e ?s/New/Old/g? *.html
2009-11-16 13:40:13
User: chappado
Functions: perl
-4

-p -> loop (same as -n in sed)

-i -> edit files

-e -> execute command

replace Old with New in all *.html files

utime(){ perl -e "print localtime($1).\"\n\"";}
2009-11-06 12:58:10
User: MoHaG
Functions: perl
1

A shell function using perl to easily convert Unix-time to text.

Put in in your ~/.bashrc or equivalent.

Tested on Linux / Solaris Bourne, bash and zsh. using perl 5.6 and higher.

(Does not require GNU date like some other commands)

git ls-files | xargs -n1 -d'\n' -i git-blame {} | perl -n -e '/\s\((.*?)\s[0-9]{4}/ && print "$1\n"' | sort -f | uniq -c -w3 | sort -r
2009-10-25 01:44:03
User: askedrelic
Functions: perl sort uniq xargs
Tags: statistics git
3

Figures out total line contribution per author for an entire GIT repo. Includes binary files, which kind of mess up the true count.

If crashes or takes too long, mess with the ls-file option at the start:

git ls-files -x "*pdf" -x "*psd" -x "*tif" to remove really random binary files

git ls-files "*.py" "*.html" "*.css" to only include specific file types

Based off my original SVN version: http://www.commandlinefu.com/commands/view/2787/prints-total-line-count-contribution-per-user-for-an-svn-repository

perl -ne '$pkg=$1 if m/^Package: (.*)/; print "$1\t$pkg\n" if m/^Installed-Size: (.*)/;' < /var/lib/dpkg/status | sort -rn | less
2009-10-19 12:55:59
User: hfs
Functions: perl sort
0

List packages and their disk usage in decreasing order. This uses the "Installed-Size" from the package metadata. It may differ from the actual used space, because e.g. data files (think of databases) or log files may take additional space.