Linux Grep Command Examples

Linux Grep Command Examples.
Linux Grep Command Examples.

grep command in linux with examplesIn this article, we are going to provide you with useful examples of the Linux grep command, including the two variant programs: egrep and fgrep. grep (global regular expression print) is used to search for text strings and regular expressions line by line which matches a specified pattern within one or more files.

Listed below are some simple and useful examples of how to use the grep command on Linux and search for a string in one/multiple files.

1. Grep Command Example on how to search for lines in a specific file

Grep Command in Linux ExamplesSearch for lines containing ‘DB_USER’ in WordPress configuration file (wp-config.php):

# grep 'DB_USER' wp-config.php
define('DB_USER', 'wpuser');

2. Grep Command Example on how to search for specific text in all files

Search for all PHP files containing ‘str_replace’ text inside the ‘wp-admin’ directory of a WordPress installation:

# # grep "str_replace" admin*.php
admin-ajax.php: add_action( 'wp_ajax_' . $_GET['action'], 'wp_ajax_' . str_replace( '-', '_', $_GET['action'] ), 1 );
admin-ajax.php: add_action( 'wp_ajax_' . $_POST['action'], 'wp_ajax_' . str_replace( '-', '_', $_POST['action'] ), 1 );
admin-header.php:$admin_body_class .= ' branch-' . str_replace( array( '.', ',' ), '-', floatval( $wp_version ) );
admin-header.php:$admin_body_class .= ' version-' . str_replace( '.', '-', preg_replace( '/^([.0-9]+).*/', '$1', $wp_version ) );
admin-header.php:$admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );

4. Grep Command Example on how to add line numbers to search results

If you want to add line numbers to search results, use the following command:

# grep -n "str_replace" admin*.php
admin-ajax.php:73:      add_action( 'wp_ajax_' . $_GET['action'], 'wp_ajax_' . str_replace( '-', '_', $_GET['action'] ), 1 );
admin-ajax.php:76:      add_action( 'wp_ajax_' . $_POST['action'], 'wp_ajax_' . str_replace( '-', '_', $_POST['action'] ), 1 );
admin-header.php:157:$admin_body_class .= ' branch-' . str_replace( array( '.', ',' ), '-', floatval( $wp_version ) );
admin-header.php:158:$admin_body_class .= ' version-' . str_replace( '.', '-', preg_replace( '/^([.0-9]+).*/', '$1', $wp_version ) );
admin-header.php:160:$admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );

5. Grep Command Example on how to search in all files for specific text recursively

Search for all PHP files whose contents mention ‘str_replace’ text which has either capital or small letters in it, located in the ‘wp-admin’ directory recursively (i.e. read all files under each directory) and listsLinux grep command and Examples the file names only:

# grep -ril "str_replace" admin*.php
admin-ajax.php
admin-header.php

grep command is often used in a Unix/Linux pipeline with other commands. Listed below are some more advanced examples of how to use the grep command on Linux:

6. Grep Command Example on how to search for lines in the current directory

how to use grep commandSearch for lines containing ‘eval’ in all PHP files located in the current directory:

#find . -name '*.php' -exec grep -l 'eval' {} \;
./wp-admin/includes/image.php
./wp-admin/includes/class-wp-upgrader.php
./wp-admin/includes/class-pclzip.php
./wp-admin/includes/media.php
./wp-admin/includes/update-core.php
./wp-admin/includes/ajax-actions.php
./wp-admin/includes/class-wp-automatic-updater.php
./wp-admin/includes/class-wp-posts-list-table.php
./wp-admin/user-edit.php
./wp-admin/edit-tag-form.php

6. Grep Command Example on how to list all Apache processes

List all Apache processes:

# ps aux | grep http
root      1259  0.0  0.0 114644   984 pts/0    S+   01:37   0:00 grep --color=auto http
root      1471  0.0  2.3 502008 24508 ?        Ss   Jun27   0:03 /usr/sbin/httpd -k start
nobody   31256  0.0  3.8 510140 40412 ?        S    00:00   0:01 /usr/sbin/httpd -k start
nobody   31257  0.0  4.2 510220 44404 ?        S    00:00   0:02 /usr/sbin/httpd -k start
nobody   31258  0.0  4.5 511156 47924 ?        S    00:00   0:02 /usr/sbin/httpd -k start
nobody   31259  0.0  5.4 514240 56648 ?        S    00:00   0:02 /usr/sbin/httpd -k start
nobody   31260  0.0  3.9 510388 41668 ?        S    00:00   0:01 /usr/sbin/httpd -k start
nobody   31585  0.0  4.2 511156 44188 ?        S    00:13   0:01 /usr/sbin/httpd -k start

7. Grep Command Example on how to see which socket belongs to process ID

See which sockets belong to process ID 31585:

# lsof -p 31585 | grep -Ei 'cwd|unix|sock'
httpd   31585 nobody  cwd    DIR    144,142     4096   35120509 /
httpd   31585 nobody  mem    REG       8,18            38427466 /opt/cpanel/ea-php56/root/usr/lib64/php/modules/sockets.so (path dev=144,142)
httpd   31585 nobody  mem    REG       8,18            36064050 /usr/lib64/apache2/modules/mod_unixd.so (path dev=144,142)

7. Grep Command Example on how to display the total number of connections on a specific port

grep commandDisplay total number of Apache connections on port 80:

# netstat -an | grep :80 |wc -l
1627

8. Grep Command Example on how to remove all frozen messages in Exim mail queue

Remove all frozen messages in Exim mail queue immediately:

#exim -bpr | grep frozen | awk {'print $3'} | xargs exim -Mrm
Message 1dPhFh-0000t6-D0 has been removed
Message 1dPenR-0000Ls-S8 has been removed
Message 1dPexx-0000OD-A6 has been removed

9. Grep Command Example on how to find a file with text pattern

Find files containing a text pattern (e.g. hello):

find . -iname "*.txt" -exec grep -l "hello" {} +

To search for multiple patterns at one time, you can simply use the egrep command. egrep is the same as grep -E.

#egrep 'cachedir|exclude' /etc/yum.conf
cachedir=/var/cache/yum/$basearch/$releasever
exclude=lm_sensors*

The fgrep searches a file or list of files for a fixed pattern string. fgrep is the same as grep –F.

#fgrep 'cachedir' /etc/yum.conf
cachedir=/var/cache/yum/$basearch/$releasever

using grep command in LinuxOf course, you don’t have to learn the grep commands in Linux, if you use one of our managed & optimized VPS Hosting services, in which case you can simply ask our expert Linux admins to help you with grep command on your Linux server. They are available 24×7 and will take care of your request immediately. You can also read our post about Linux Grep Guide for Advanced Users.

PS. If you liked this post about Linux grep Command Examples, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Leave a Comment