A more powerful version of grep

The utility command grep has a more powerful brother that let us use POSIX Extended Regular Expressions.

The utility grep is extremely useful when we want to find something specific in files, manuals or in combination with ls to find files and directories. It's possible to do some simple searches with grep, here are two examples.

Find the database settings in a WordPress installation.

$ grep DB_ wp-config.php
define('DB_NAME', 'database');
define('DB_USER', 'username');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'hostname');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');

Find all the .log files in the current directory.

$ ls -l | grep .log$
-rw-r-----  1 syslog  adm  Jan 31 21:55 auth.log
-rw-r-----  1 syslog  adm  Jan 31 21:55 mail.log
-rw-r-----  1 mysql   adm  Jan 31 21:55 mysql.log

But how can we use more powerful regexp expressions. In the same way? No. Or Yes.

To use POSIX Extended Regular Expressions we need to add an option flag to grep, namely -E. Alternatively we can use the egrep command, which is a bit smoother. Initially I had no idea that egrep existed. I was tearing my hair out every time, until I found it.

A practical example

Let's say we have a directory with some files for a simple website.

$ ls -l
-rw-r--r--  1 jdoe  jdoe  Jan 31 21:55 about.html
-rw-r--r--  1 jdoe  jdoe  Jan 31 21:55 app.js
-rw-r--r--  1 jdoe  jdoe  Jan 31 21:55 contact.html
-rw-r--r--  1 jdoe  jdoe  Jan 31 21:55 data.json
-rw-r--r--  1 jdoe  jdoe  Jan 31 21:55 index.html
-rw-r--r--  1 jdoe  jdoe  Jan 31 21:55 logo.png
-rw-r--r--  1 jdoe  jdoe  Jan 31 21:55 style.css

Now we want to list all files that end with .html or .js. This is how to do it.

$ ls -l | egrep "\.(html|js)$"
-rw-r--r--  1 jdoe  jdoe  Jan 31 21:55 about.html
-rw-r--r--  1 jdoe  jdoe  Jan 31 21:55 app.js
-rw-r--r--  1 jdoe  jdoe  Jan 31 21:55 contact.html
-rw-r--r--  1 jdoe  jdoe  Jan 31 21:55 index.html