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
for f in `find . -name "*.php"`; do perl -p -i.bak -e 's/<\?php \/\*\*\/ eval\(base64_decode\(\"[^\"]+"\)\);\?>//' $f; done
2012-03-12 10:44:33
User: lizuka
Functions: perl
0

loop through files in .php extension

make a .bak of each of them

find the base64_decode function and remove it

perl -e "print 'yes' if `exim -bt $s_email_here | grep -c malformed`;"
2012-02-28 04:42:41
User: DewiMorgan
Functions: perl
0

People are *going* to post the wrong ways to do this. It's one of the most common form-validation tasks, and also one of the most commonly messed up. Using a third party tool or library like exim means that you are future-proofing yourself against changes to the email standard, and protecting yourself against the fact that actually checking whether an email address is valid is *not possible*.

Still, perhaps your boss is insisting you really do need to check them internally. OK.

Read the RFCs. The bet before the @ is specified by RFC2821 and RFC2822. The domain name part is specified by RFC1035, RFC1101, RFC1123 and RFC2181.

Generally, when people say "email address", they mean that part of the address that the RFC terms the "addr-spec": the "blah@domain.tld" address, with no display names, comments, quotes, etc. Also "root@localhost" and "root" should be invalid, as should arbitrary addressing schemes specified by a protocol indicator, like "jimbo@myprotocol:foo^bar^baz".

So... With the smallest poetic license for readability (allowing underscores in domain names so we can use "\w" instead of "[a-z0-9]"), the RFCs give us:

^(?:"(?:[^"\\]|\\.)+"|[-^!#\$%&'*+\/=?`{|}~.\w]+)@(?=.{3,255}$)(?:[\w][\w-]{0,62}\.){1,128}[\w][\w-]{0,62}$

Not perfect, but the best I can come up with, and most compliant I've found. I'd be interested to see other people's ideas, though. It's still not going to verify you an address fersure, properly, 100% guaranteed legit, though. What else can you do? Well, you could also:

* verify that the address is either a correct dotted-decimal IP, or contains letters.

* remove reserved domains (.localhost, .example, .test, .invalid), reserved IP ranges, and so forth from the address.

* check for banned domains (whitehouse.gov, example.com...)

* check for known TLDs including alt tlds.

* see if the domain has an MX record set up: if so, connect to that host, else connect to the domain.

* see if the given address is accepted by the server as a recipient or sender (this fails for yahoo.*, which blocks after a few attempts, assuming you are a spammer, and for other domains like rediffmail.com, home.com).

But these are moving well out of the realm of generic regex checks and into the realm of application-specific stuff that should be done in code instead - especially the latter two. Hopefully, this is all you needed to point out to your boss "hey, email validation this is a dark pit with no bottom, we really just want to do a basic check, then send them an email with a link in it: it's the industry standard solution."

Of course, if you want to go nuts, here's an idea that you could do. Wouldn't like to do it myself, though: I'd rather just trust them until their mail bounces too many times. But if you want it, this (untested) code checks to see if the mail domain works. It's based on a script by John Coggeshall and Jesse Houwing that also asked the server if the specific email address existed, but I disliked that idea for several reasons. I suspect: it will get you blocked as a spambot address harvester pretty quick; a lot of servers would lie to you; it would take too much time; this way you can cache domains marked as "OK"; and I suspect it would add little to the reliability test.

// Based on work by: John Coggeshall and Jesse Houwing.

// http://www.zend.com/zend/spotlight/ev12apr.php

mailRegex = '^(?:"(?:[^"\\\\]|\\\\.)+"|[-^!#\$%&\'*+\/=?`{|}~.\w]+)';

mailRegex .= '@(?=.{3,255}$)(?:[\w][\w-]{0,62}\.){1,128}[\w][\w-]{0,62}$';

function ValidateMail($address) {

  global $mailRegex; // Yes, globals are evil. Put it inline if you want.

  if (!preg_match($mailRegex)) {

    return false;

  }

  list ( $localPart, $Domain ) = split ("@",$Email);

  // connect to the first available MX record, or to domain if no MX record.

  $ConnectAddress = new Array();

  if (getmxrr($Domain, $MXHost)) {

    $ConnectAddress = $MXHost;

  } else {

    $ConnectAddress[0] = $Domain;

  }

  // check all MX records in case main server is down - may take time!

  for ($i=0; $i < count($ConnectAddress); $i++ ) {

    $Connect = fsockopen ( $ConnectAddress[$i], 25 );

    if ($Connect){

      break;

    }

  }

  if ($Connect) {

    socket_set_blocking($Connect,0);

    // Only works if socket_blocking is off.

    if (ereg("^220", $Out = fgets($Connect, 1024))) {

      fclose($Connect); // Unneeded, but let's help the gc.

      return true;

    }

    fclose($Connect); // Help the gc.

  }

  return false;

}

svn st | awk ' {if ( $1 == "?" ){print $1="",$0}} ' | sed -e 's/^[ \t]*//' | sed 's/ /\\ /g' | perl -ne '`svn add ${1}@` if /(.*)(@*)(.*)/'
2012-02-23 18:42:02
Functions: awk perl sed
Tags: svn perl
0

handels @, ?, whitespaces in names.

replace "?" and "add" by "!" and "rm" for svn mass remove.

---> I m looking for a nicer way to write it, perhaps with something using " perl -ne '`blahblah` if /\?(.*)/' "

tail -f production.log | perl -ne 'if (/^Completed.in.(\d+)/){$d = int($1/1000);print "\n";$f{$d}++;for $t (sort(keys(%f))){print $t."s: ".$f{$t}."\n"}}'
2012-02-23 14:37:33
User: theist
Functions: perl tail
0

Commandline perl filter for, using a production.log from a rails app, display on realtime the count of requests grouped by "seconds to complete" (gross round, but fair enough for an oneliner) :)

perl -le 'print ~~ map {-s} <*>'
2012-02-21 21:09:48
User: MarxBro
Functions: perl
Tags: perl find wc
7

Just want to post a Perl alternative.

Does not count hidden files ('.' ones).

perl -e 'rename $_, s/ /-/gr for <*.mp3>'
perl -e 'for (<*.mp3>) { $old = $_; s/ /-/g; rename $old, $_ }'
#(see sample) $ cat x | perl -pe 'BEGIN{ print "TIME;...\n"; } s!(\S+) - (\S+) - \[(\d\d)/(\S\S\S)/(\S+):(\d\d):(\d\d:\d\d) \S+\] "(\S+) (.*/)(\S+)(?:\.([^?]*)(\?\S*)?) HTTP/\S+" (\d+) (\S+)!$3-$4-$5 $6:$7;$6;$2;$1;$8;$13;1;$14;$11;$10;$9;$12;!' > x.csv
2012-02-10 16:58:50
User: hute37
Functions: cat perl
2

- excel date compatible with a separate hour field

- added a fixed 1 for easier request counter aggregation

- split URL in directory, filename, fileext, query

- used with tomcat valve with response bytes replaced by elapsed time

du -k | sort -n | perl -ne 'if ( /^(\d+)\s+(.*$)/){$l=log($1+.1);$m=int($l/log(1024)); printf ("%6.1f\t%s\t%25s %s\n",($1/(2**(10*$m))),(("K","M","G","T","P")[$m]),"*"x (1.5*$l),$2);}' | more
2012-02-07 15:49:19
User: Q_Element
Functions: du perl printf sort
0

This one line Perl script will display the smallest to the largest files sizes in all directories on a server.

useradd -m -p $(perl -e'print crypt("passwordscelta", "stigghiola")') user
2012-02-06 19:53:01
User: 0disse0
Functions: perl useradd
0

The crypt function takes a password, key, as a string, and a salt character array which is described below, and returns a printable ASCII string which starts with another salt. It is believed that, given the output of the function, the best way to find a key that will produce that output is to guess values of key until the original value of key is found.

from http://en.wikipedia.org/wiki/Crypt_(Unix)

perl -i -ne 'print if $. == 3..5' <filename>
genRandomText() { perl -e '$n=shift; print chr(int(rand(26)) + 97) for 1..$n; print "\n"' $1;}
2012-01-21 00:21:20
User: putnamhill
Functions: perl
-1

If you don't have seq, you can use perl.

perl -p -i -e 's/.*\n//g if $.==2' ~/.ssh/known_hosts
perl -le 'print$_%3?$_%5?$_:"Buzz":$_%5?"Fizz":"FizzBuzz"for 1..100'
comm -13 <(od -vw1 -tu1 dummy.txt|cut -c9-|sort -u) <(seq 0 127|sort)|perl -pe '$_=chr($_)'|od -c
2012-01-09 01:32:20
User: bazzargh
Functions: comm cut od perl seq sort
Tags: bash
0

Search in decimal rather than hex. od dumps the character list, cut to remove offsets, sort -u gives the used characters. seq gives the comparison list, but we need this sorted alphabetically for comm, which does the filtering. I drop to perl to convert back to characters (is there a better way?) and then use od to dump them in a print-safe format.

find . -type f|perl -lne '@x=sort {$b->[0]<=>$a->[0]}[(stat($_))[7],$_],@x;splice(@x,11);print "@{$x[0]}";END{for(@x){print "@$_"}'
2012-01-08 14:43:43
User: bazzargh
Functions: find perl
Tags: sort perl find
-2

A different approach to the problem - maintain a small sorted list, print the largest as we go, then the top 10 at the end. I often find that the find and sort take a long time, and the large file might appear near the start of the find. By printing as we go, I get better feedback. The sort used in this will be much slower on perls older than 5.8.

perl -e '$f = join("", <>); for (0..127) {$_ = chr($_); if (/[[:print:]]/) {print if index($f, $_) < 0}} print "\n"'
2012-01-05 23:38:06
User: putnamhill
Functions: perl
Tags: perl slurp
0

Here's a perl version that only considers printable characters. Change the regex /[[:print:]]/ to look for different sets of delimiter characters.

perl -e "binmode(STDOUT, ':utf8'); print \"$@\""; echo # newline
2012-01-02 10:34:51
User: mathias
Functions: echo perl
0

This is especially useful to get crazy stuff like space characters copied to your pasteboard correctly.

Source: https://github.com/mathiasbynens/dotfiles/blob/master/.functions

perl -ne 's/\^.{1,7}?m//g;print'
2012-01-02 01:32:33
User: Tracerneo
Functions: perl
Tags: perl regex colors
-1

Removes special characters (colors) in '^]]Xm' and '^]]X;Ym' format from file.

Use pipe ('input | perl [...]') or stream ('perl [...]

You can use 'cat -v infile' as 'input' to show special characters instead of interpreting (there is problem with non-ASCII chars, they are replaced by M-[char]).

cvs -Q status | perl -ne 'print if m/^File.+Status: (?!Up-to-date)/ .. m/^=/;'
2011-12-07 00:33:49
User: dexterp
Functions: cvs perl
0

This will also print the path to file which is not included in the other examples.

find . -iregex ".+\.\(c\|cpp\|h\)" | xargs -I{} perl -e "system(\"iconv -f SHIFT_JIS -t UTF-8 {} > temp; mv temp {} \");" ;
lsmod | perl -e 'print "digraph \"lsmod\" {";<>;while(<>){@_=split/\s+/; print "\"$_[0]\" -> \"$_\"\n" for split/,/,$_[3]}print "}"' | dot -Tpng | display -
2011-11-27 14:02:35
User: bandie91
Functions: dot lsmod perl
35

parse `lsmod' output and pass to `dot' drawing utility then finally pass it to an image viewer

perl -MO=Deparse filename.pl | perltidy > new.pl
2011-11-16 18:54:30
User: kimmel
Functions: perl
0

This will create a new file with proper code formatting and all comments removed.

perl -le 'print scalar gmtime shift' 1234567890
perl -MExtUtils::Installed -E 'say for ExtUtils::Installed->new()->modules()'
2011-11-16 17:26:47
User: kimmel
Functions: perl
Tags: perl oneliner
0

Lists all the modules that were installed the "proper way". It also uses Perl 5.10(or higher)'s say command for less typing.