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.

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.

Universal configuration monitoring and system of record for IT.

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:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

Top Tags



Psst. Open beta.

Wow, didn't really expect you to read this far down. The latest iteration of the site is in open beta. It's a gentle open beta-- not in prime-time just yet. It's being hosted over at UpGuard (link) and you are more than welcome to give it a shot. Couple things:

  • » The open beta is running a copy of the database that will not carry over to the final version. Don't post anything you don't mind losing.
  • » If you wish to use your user account, you will probably need to reset your password.
Your feedback is appreciated via the form on the beta page. Thanks! -Jon & CLFU Team

Commands tagged regex from sorted by
Terminal - Commands tagged regex - 54 results
awk 'BEGIN{srand()} match($0, /DELTA=([0-9]+);/, a) {w[i++]=a[1]} END {print w[int(rand()*i)]}' file.name
2015-11-13 17:56:34
User: jkirchartz
Functions: awk
Tags: awk regex random

seed the random number generator,

find all matches in a file

put all matches from the capture group into an array

return a random element from the array

perl -pE's/(\S+\s*){0,1}//'
2015-05-09 15:14:58
User: pung96
Functions: perl

An advantage is that this doesn't modify remained string at all. One can change {0,1} with {0,n} to drop several columns

grep -Eoa "\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b" Filetosearch.txt
2014-11-02 19:50:54
User: jsbrown
Functions: grep

Command returns valid IP addresses. Append the following regex to additionally filter out NAT and reserved IP addresses

| grep -Ev "^0|\.0[0-9]|^10\.|^127\.|^169\.|^172\.(1[6-9]|2[0-9]|3[01])|^192.168.|^2(2[4-9]|3[0-9])|^2(4[0-9]|5[0-5])"

finger $(whoami) | perl -ne '/Name: ([a-zA-Z0-9 ]{1,})/ && print "$1\n"'
finger $(whoami) | egrep -o 'Name: [a-zA-Z0-9 ]{1,}' | cut -d ':' -f 2 | xargs echo
2014-09-24 01:22:07
User: swebber
Functions: cut egrep finger xargs

Its possible to user a simple regex to extract de username from the finger command.

The final echo its optional, just for remove the initial space

ifconfig | egrep -A2 "eth|wlan" | tr -d "\n"| sed 's/\-\-/\n/g'|awk '{print "mac: "$5 " " $7}' | sed 's/addr:/addr: /g'
ifconfig | head -n 2 | tr -d '\n' | sed -n 's/.*\(00:[^ ]*\).*\(adr:[^ ]*\).*/mac:\1 - \2/p'
hl() { while read -r; do printf '%s\n' "$(perl -p -e 's/('"$1"')/\a\e[7m$1\e[0m/g' <<< "$REPLY")"; done; }
find * -regextype posix-extended -regex '.*\.(ext_1|ext_2)' -exec cp {} copy_target_directory \;
find . -type f -regex ".*/core.[0-9][0-9][0-9][0-9]$"
2014-01-17 16:44:47
User: H3liUS
Functions: find

Will find and list all core files from the current directory on. You can pass | xargs rm -i to be prompted for the removal if you'd like to double check before removal.

curl ${URL} 2>/dev/null|grep "<${BLOCK}>"|sed -e "s/.*\<${BLOCK}\>\(.*\)\<\/${BLOCK}\>.*/\1/g"
2013-08-31 14:53:54
User: c3w
Functions: grep sed

set BLOCK to "title" or any other HTML / RSS / XML tag and curl URL to get everything in-between e.g. some text

find . -type f -name filename.exe -exec sed -i "s/oldstring/oldstring/g" {} +;
grep '^[^#]' squid.conf
2013-02-21 18:51:06
User: Flow
Functions: grep
Tags: regex grep,

grep '^[^#]' sample.conf

\__/ |||| \_________/

| |||| |

| |||| \- Filename

| ||||

| |||\- Only character in group is '#'

| |||

| ||\- Negate character group (will match any cahracter *not* in the

| || group)

| ||

| |\- Start new character group (will match any character in the

| | group)

| |

| \- Match beginning of line


\- Run grep

Empty lines will also be not matched, because there has to be at least one non-hash-sign character in the line.

diff -BI '^#' file{1,2}
diff -u <(grep -vE '^(#|$)' file1) <(grep -vE '^(#|$)' file2)
2013-02-12 13:59:39
Functions: diff grep

Runs a diff on two files ignore comments and blank lines (diff -I=RE does not work as expected). Adapted from a post found on stackexchange.

curl -s -d "CSField=Name" -d "CSInput=BostonUCompSci" http://torstatus.blutmagie.de/index.php | grep -oP "ip=\K(\d+)(\.\d+){3}"
2012-03-09 16:52:27
User: JisSey
Functions: grep

Reciprocally, we could get the node name from a give Tor IP address =>

ip2node() { curl -s -d "QueryIP=$1" http://torstatus.blutmagie.de/tor_exit_query.php | grep -oP "Server name:.*'>\K\w+" ; }



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

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 "[email protected]" address, with no display names, comments, quotes, etc. Also "[email protected]" and "root" should be invalid, as should arbitrary addressing schemes specified by a protocol indicator, like "[email protected]: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:


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




  if ($Connect) {


    // 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;


2012-02-28 02:54:57
User: DewiMorgan
Tags: regex parsing

Sometimes, especially when parsing HTML, you want "all text between two tags, that doesn't contain another tag".

For example, to grab only the contents of the innermost <div>s, something like:


...may be your best option to capture that text.

It's not always needed, but is a powerful arrow in your regex quiver in those cases when you do need it.

Note that, in general, regular expressions are the Wrong Choice for parsing HTML, anyway. Better approaches are solutions which let you navigate the HTML as a proper DOM. But sometimes, you just need to use the tools available to you. If you don't, then you have two problems.

s/(?=.{75,})(?:(.{0,75})(?:\r\n?|\n\r?)|(.{0,75}))[ ]/\1\2\n /g
2012-02-28 02:27:20
User: DewiMorgan
Tags: regex

With thanks to dew on Efnet's #regex, back in 2005.

This version indents subsequent lines after the first by one space, to make paragraphs visibly obvious -- remove the \3 to prevent this behavior.

Lines are only broken at spaces: long strings with no spaces will not wrap, so URLs are safe.

Replace the "75"s to make the regex linewrap to other amounts.

From the unix commandline, "fold" is likely your better choice, but this snippet is handy in editors which allow regular expressions, in scripting, and other such situations where "fold" is unavailable.

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

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

yum remove \*.i\?86
grep -n log4j MainPm.java | sed -e 's/^\([^:]*\):\(.*\)/\2 \1/'
2011-10-21 12:50:30
User: bash_vi
Functions: grep sed
Tags: sed grep regex

Uses sed with a regex to move the linenumbers to the line end. The plain regex (w/o escapes) looks like that:


aptitude search ^tin
2011-10-20 17:51:36

# Search for an available package on Debian systems using a regex so it only matches packages starting with 'tin'.

sed -i 's/[ \t]\+$//g' file.txt
2011-09-07 01:47:44
User: elder
Functions: sed
Tags: sed regex

This command is useful when you are programming, for example.

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.