• Skip to main content
  • Skip to primary sidebar
  • Home
  • WordPress
  • web Hosting
  • linux
  • mysql
  • nginx
  • apache2
  • devops

Raju Ginni

wordpress tutorials seo hosting etc

You are here: Home / Linux sysadmin tutorials linux system administrator / Top 10 GREP Commands in linux to search files directory words strings

Top 10 GREP Commands in linux to search files directory words strings

Table of Contents

Toggle
      • Grep command in linux explained
      • Options Description
      • Expressions
    • Finding a text in list of files in a directory.
      • grep ‘word’ file1 file2 file3
  • Grep From Files and Display the File Name On Linux
      • Searching a String / word in files & directories
    • grep command to search string word in file
    • finding a file containing a particular text string
    •  use grep recursively
      • root@instance-1:~# grep . /proc/sys/net/ipv4/tcp*mem
    • linux find a file in a directory
    • find command to find a file in a directory
    • finding log errors with tail and grep command

Grep command in linux explained

Grep command used for search files and search pattern. (Find Text in Files).
grep command stands for “global regular expression print
grep [options] pattern [files]

Options Description

-c : counts the frequency only
-h : Display the matched lines, but do not display the filenames.
-i : case insensitive Ignores, case for matching
-l : filenames only.(imp)
-n :  line number if word in a file
-v : lines that are not matched with the keyword
-e exp : Specifies expression with this option. Can use multiple times.
-f file : takes the file patterns
-E : Treats pattern as an extended regular expression (ERE)
-w : prints whole words in a file
-o : Print only matched words per line

Expressions

Start ^
End $
grep “^linux” mynewfile.txt
grep “ux$” mynewfile.txt
$grep –e “admin” –e “admin” –e “admin” newfile.txt
$grep –f newfile.txt  mynewfile.txt

Finding a text in list of files in a directory.

grep ‘word’ file1 file2 file3

grep -n ‘root’ /etc/passwd
grep –color root /etc/passwd

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

grep -l “string” filenamegrep -l root /etc/*

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

grep -R “192.168.0.1” /etc/
grep -R ‘string’ dir/
grep -R ‘expires’ /etc/nginx/
root@instance-1:/etc/nginx# grep -R ‘expires’ /etc/nginx/
/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;
grep -R ‘add_header’ /etc/nginx/
grep -nr ‘ssl’ .
t@instance-1:/etc/nginx# grep -nr ‘ssl’ .
./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;
The dot at the end searches the current directory. Meaning for each parameter:
-n Show relative line number in the file
‘yourString*’ String for search, followed by a wildcard character
-r Recursively search subdirectories listed
. Directory for search (current directory)
cd /etc/nginx/sites-enabled#
grep -nr ‘ssl’ .
./eaadharcard.co.in.conf:8: ssl on;
./eaadharcard.co.in.conf:9: ssl_certificate /etc/nginx/ssl/eaadharcard.co.in.pem;
./eaadharcard.co.in.conf:10: ssl_certificate_key /etc/nginx/ssl/eaadharcard.co.in.key;
./employmentnewsdaily.in:5: ssl on;

root@instance-1:~# grep . /proc/sys/net/ipv4/tcp*mem

/proc/sys/net/ipv4/tcp_mem:45525        60702   91050
/proc/sys/net/ipv4/tcp_rmem:4096        131072  6291456
/proc/sys/net/ipv4/tcp_wmem:4096        16384   4194304
root@instance-1:~#/etc/nginx/sites-enabled# grep -nr ‘rajuginni.com‘ .

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

i want to ind newrelic.ini file  this directory
find /etc/php/7.4/fpm/conf.d/ newrelic
ls -l  /etc/php/7.4/fpm/conf.d/
2nd way
cd /etc/php/7.4/fpm/conf.d/
grep newrelic
/etc/php/7.4/fpm/conf.d

finding log errors with tail and grep command

tail -f /var/log/file.log | grep search_term
tail -f /var/log/nginx/error.log | grep 503
tail -f  /www/test/error_log
tail -f www/test/error_log | grep 503
tail -f www/test/error_log | grep error
tail -f www/test/error_log | grep conn

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'

Primary Sidebar

Linux sysadmin tutorials linux system administrator

  • top 10 apt & apt-get commands (most used) apt vs apt-get
  • If-Else Statements in Shell Scripting
  • linux commands pdf (files & Directories, zip & unzip process, search etc)
  • Find Files with Specific Text on Linux grep find command
  • linux performance tuning inode limit file descriptors tco, kernel etc
  • Variables and Data Types in Shell Scripting
  • Top 10 most used Cat commands with examples (create, view, append files)
  • Ip tables / ufw / firewall d commands for block port ip rate limiting
  • Top 10 zip / tar commands to compress & extract files in linux
  • TOP 10 mv & cp commands in linux to move & copy files in Linux
  • Top 10 GREP Commands in linux to search files directory words strings
  • lsof netstat commands to know listening ports in linux 3 ways
  • Upgrade Ubuntu from 18.04 (19.10) to 20.04 LTS command line or gui server | desktop
  • 3 Ways (SCP, rsync, Sftp) linux server migration between two remote server apache nginx
  • linux system specs commands (CPU, Memory, Disk )speed, type. manufacture
  • linux sysctl command tweaks & hardening
  • linux security limits.conf deciding user limits process limits for nginx server
  • ulimit linux unlimited command unlimto set & know user limits open files file descriptor max user process etc.
  • red hat linux certification cost jobs salary syllabus courses fees
  • ufw firewall commads allow port enable disable ubuntu 20.04
  • ddos attack prevention
  • change ssh port in linux - avoid sshd ddos attacks
  • ping command
  • memcached install ubuntu wordpress
  • check linux version (lsb_release -a) ubuntu debian 32 or 64 bit
  • rsync command linux with examples comparison to scp
  • how to uninstall package in linux ubuntu rpm, yum apt-get
  • increase open file limit linux File descriptor ft nginx , mysql, lemp
  • remove repository ubuntu
  • htop commad memory details virtual vs shard vs resident
  • chown command in Linux with Examples
  • Kill PHP process
  • VIrtual Memory vs RSS Memory vs Shared memory in Linux
  • oom killer fixing it by configuration linux ubuntu
  • Install Lemp nginx mysql php fpm Stack on Debian 11 with repository
  • connect two remote servers linux command line
  • auto start after oom killer Mysql & php fpm nginx etc ubuntu wth systemd or cron job
  • load average Linux 1, 5, 15 min 2,4,8 cores explained
  • Control Structures in Shell Scripting
  • Shell Scripting Roadmap for Beginners to Advanced
  • awk commands with practical examples
  • Shell Scripting Tutorial for Beginners 🚀
  • find Command in Linux with Examples
  • sed Command in Linux with Examples (Beginner to Advanced)
  • Linux Text processing commands in with Examples
  • linux disk management commands
  • fdisk command in linux with examples
  • how to add a new disk in linux
  • Linux mount Command with Examples
  • fstab options with examples
  • Top 50 Shell Scripting Interview Questions and Answers
  • Linux Networking Interview Questions and Answers
  • Linux Networking Commands Cheat Sheet with Examples pdf
  • Netstat & SS Commands cheat sheet with examples Interview Questions
  • Nmap Cheat Sheet – Network Scanning & Security
  • Bash Brackets ([], (), {}, $( ), $(( ))) – Types, Uses & Examples

hi i am raju ginni, primalry i manage wordpress websites on GCP cloud platform as a cloud engineer, and create content on passionate things.
you can follow me on youtbe

© 2025 - All Rights Reserved Disclaimer & Privacy Policy