Grep command in linux explained
Options Description
Expressions
Finding a text in list of files in a directory.
grep ‘word’ file1 file2 file3
cd /etc/nginx/sites-enabled
grep job.sarkari *
root@murali:/etc/nginx/sites-enabled# grep job.sarkari *
stories:server_name job.sarkariresultz.in;
searches only current directory
grep -rnw ‘/path/to/somewhere/’ -e ‘pattern’
-r or -R is recursive,
-n is line number, and
-w stands for match the whole word.
-l (lower-case L) can be added to just give the file name of matching files.
-e is the pattern used during the search
Along with these, –exclude, –include, –exclude-dir flags could be used for efficient searching:
Grep From Files and Display the File Name On Linux
Searching a String / word in files & directories
root@instance-1:/etc/nginx# grep -nr ‘cache-control’ .
./conf.d/global.config:62:add_header cache-control “max-age=31536000”;
./conf.d/expires.global:5:add_header cache-control: max-age=31536000;
grep command to search string word in file
root@-s-4vcpu-8gb-blr1-01:~# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
grep WP_CACHE_KEY_SALT /var/www/html/yoursite.com/wp-config.php
all files containing string
grep -R containing text /home/user/
grep ‘string1 string2’ filename
finding a file containing a particular text string
grep “find me in home” /home/tom/*.txt
grep “find me anywhere in text files “~/*.txt
root@murali:~# grep “shivajobs.in” /etc/nginx/sites-enabled/*
/etc/nginx/sites-enabled/shivafile:server_name file.shivajobs.in;
/etc/nginx/sites-enabled/shivajobs.in:root /var/www/html/shivajobs.in/;
/etc/nginx/sites-enabled/shivajobs.in:server_name shivajobs.in www.shivajobs.in;
/etc/nginx/sites-enabled/shivajobs.in:include /var/www/html/shivajobs.in/nginx.conf;
/etc/nginx/sites-enabled/shivajobs.in: if ($host = shivajobs.in) {
/etc/nginx/sites-enabled/shivajobs.in:server_name shivajobs.in shivajobs.in;
root@murali:~#
use grep recursively
/etc/nginx/rocket-nginx/default.conf: expires 30d;
/etc/nginx/rocket-nginx/default.conf: expires 30d;
/etc/nginx/rocket-nginx/default.conf: expires 30d;
/etc/nginx/rocket-nginx/rocket-nginx.tmpl: expires 30d;
/etc/nginx/rocket-nginx/rocket-nginx.tmpl: expires 30d;
/etc/nginx/rocket-nginx/rocket-nginx.tmpl: expires 30d;
/etc/nginx/sites-enabled/rajuginne: # Directives to send expires headers and turn off 404 error logging.
/etc/nginx/sites-enabled/rajuginne: expires 24h;
/etc/nginx/conf.d/default.conf: expires 31536000s;
/etc/nginx/conf.d/default.conf: expires 3600s;
/etc/nginx/conf.d/default.conf: expires 31536000s;
/etc/nginx/conf.d/headers.conf:#add_header expires $expires;
/etc/nginx/conf.d/headers.conf:map $sent_http_content_type $expires {
/etc/nginx/conf.d/browsercaching.confg: expires 3600s;
/etc/nginx/conf.d/expires.global: expires 365d;
/etc/nginx/conf.d/global2.config: expires max;
./nginx.conf:38: #ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
./nginx.conf:39: #ssl_prefer_server_ciphers on;
./rocket-nginx/README.md:153:**<a name=’faq_ssl’>Will Rocket-Nginx work if my we bsite uses a SSL certificate (https) ?</a>**
./sites-enabled/f.in.conf:5:#ssl on;
./sites-enabled/ff.in.conf:6:ssl_certificate /etc/nginx/ssl/ssc.pem;
./sites-enabled/ff.in.conf:7:ssl_certificate_key /etc/nginx/ssl/ssc.key;
./sites-enabled/ffff.in:5:#listen 443 ssl http2;
./sites-enabled/.in:8:#include /etc/letsencrypt/options-ssl-nginx. conf;
root@instance-1:~# grep . /proc/sys/net/ipv4/tcp*mem
linux find a file in a directory
The find command can only filter the directory hierarchy based on a file’s name and metadata. If you need to search based on the file’s content, use a tool like grep. Consider the following example:
find . -type f -exec grep “example” ‘{}’ \; -print
find command to find a file in a directory
find /path option filename
finding log errors with tail and grep command
As the output of grep
is buffered, Add –line-buffered to grep, and that may reduce the delay for you. Very useful in some case
tail -f /path/to/log | grep --line-buffered 'X' | grep -v 'Y'
If your grep
does not have the option, you can use stdbuf
as an alternative:
tail -f /path/to/log | stdbuf -oL grep 'X' | grep -v 'Y'