Linux file files containing text

How to find all files containing specific text on Linux

How to search a directory tree for all files containing specific text string on Linux using the command line. This tutorial will help you to search all files matching a string recursively. This tutorial uses “grep” command to search string in files. Alternatively, You can also also use the find command to search files with specific string.

Syntax

grep -rwl “search-string” /path/to/serch/dir

1. Search Single String in All Files

Below example command will search string “tecadmin” in all files in /var/log directory and its sub-directories.

2. Search Multiple String in All Files

You can also specify multiple strings to search using -e switch. This is similar to egrep command. Below example will search strings “tecadmin” and “https” in all files in /var/log directory and its sub-directories.

3. Search String in Specific Files

You can search string in files matching the file name criteria. Below example command will search string “tecadmin” in files ending with .log extension in /var/log directory and its sub-directories.

If you want to exclude some files matching file name criteria. You can exclude some files using –exclude option in command. For example, do not search file ending with .txt extension.

You can also exclude some directoires to skip search inside it. For example, do not search string files inside any folder having http in their name.

Frequently Uses Command Switches

Below is the frequently uses grep command switches. To list all switches details use grep —help command.

Источник

Finding a File Containing a Particular Text String In Linux Server

Tutorial details
Difficulty level Easy
Root privileges No
Requirements grep
Est. reading time Less than 2 minutes

You need to use the grep command. The grep command or egrep command searches the given input FILEs for lines containing a match or a text string.

grep command syntax for finding a file containing a particular text string

The syntax is:
grep » text string to search » directory-path
grep [option] » text string to search » directory-path
grep -r » text string to search «directory-path
grep -r -H » text string to search » directory-path
egrep -R » word-1|word-2 » /path/to/directory
egrep -w -R » word-1|word-2 » directory-path
Let us see some common example on how to use grep to search for strings in files.

How to search and find all files for a given text string

In this example, search for a string called ‘redeem reward’ in all text (*.txt) files located in /home/tom/ directory, use:
$ grep «redeem reward» /home/tom/*.txt
OR
$ grep «redeem reward»

Task: Search all subdirectories recursively

You can search for a text string all files under each directory, recursively with -r option:
$ grep -r «redeem reward» /home/tom/
OR
$ grep -R «redeem reward» /home/tom/
Look for all files containing cacheRoot text on Linux:
grep -R cacheRoot /home/vivek/

Trying to find all files containing specific text on my Linux desktop

Task: Only display filenames

By default, the grep command prints the matching lines. You can pass -H option to print the filename for each match:
$ grep -H -r «redeem reward» /home/tom
Sample outputs:

To just display the filename use the cut command as follows:
$ grep -H -R vivek /etc/* | cut -d: -f1
Sample outputs:

Task: Suppress file names

The grep command shows output on a separate line, and it is preceded by the name of the file in which it was found in the case of multiple files. You can pass the -h option to suppress inclusion of the file names in the output:
$ grep -h -R ‘main()’

Task: Display only words

You can select only those lines containing matches that form whole words using the -w option. In this example, search for word ‘getMyData()’ only in

/projects/ dirctory:
$ grep -w -R ‘getMyData()’

Task: Search for two or more words

Use the egrep command as follows:
$ egrep -w -R ‘word1|word2’

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Task: Hide warning spam

grep command generate error message as follows due to permission and other issues:

No such file or directory
No such device or address
Permission denied

To hide all errors or warning message spam generated by the grep command, append 2>/dev/null to grep command. This will send and hide unwanted output to /dev/null device:
$ grep -w -R ‘getMyData()’

Task: Display matched text in color

Pass the —color option to the grep command display matched text/words in color on the terminal:

Fig.01: grep command in action with colors and hiding the warnings on screen

Task: Ignore case

Our final example ignore case distinctions in both the search PATTERN and the input files:
grep -i -R ‘word’ /path/to/dir
grep -i -r ‘income tax’

How do I find all files containing specific text on Linux?

The syntax is:
egrep ‘pattern’ -rnw /path/to/dir/
egrep ‘word1|word2’ -rnw /home/vivek/backups/

Finding text strings within files using grep

In this example search for lines starting with any lowercase or uppercase letter:
grep «^[a-zA-Z]» -rns

  • -r – Recursive search
  • -R – Read all files under each directory, recursively. Follow all symbolic links, unlike -r grep option
  • -n – Display line number of each matched line
  • -s – Suppress error messages about nonexistent or unreadable files
  • -w – Only work on words i.e. search only those lines containing matches that form whole words
  • -l – Show the name of each input file when match found
  • -i – Ignore case while searching

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

Last Updated: December 21st, 2019 by Hitesh J in Guides, Linux

When you are working on a server that has a big and large set of files, you must have a knowledge of grep command to search files that containing a specific string.

Find command is not capable to look inside a text file for a string.

Grep also know as a “global search for the regular expression” is a command-line utility that can be used to search for lines matching a specific string and display the matching lines to standard output.

In this tutorial, we will show you how to find files that contain specific string in Linux.

Basic Syntax of Grep to Find Strings/Text in Files/Directories

The basic syntax of grep command is shown below:

grep -irhnwl «search string» «directory-path»

Where:

  • -i : Used to ignore case sensitive string.
  • -r : Used to search directory recursively.
  • -h : Used to suppress the inclusion of the file names in the output.
  • -n : Used to display line numbers in the output.
  • -w : Used to search for matching whole words only.
  • -l : Used to display filename without matching string.

For more information about grep comamnd, run the following command:

You should see the grep manual page in the following screen:

To Search a File

To search all the lines that containing specific string in the single file use the following syntax:

grep «string» «path-of-the-file»

For example, search for a string called “nginx” in the file /etc/nginx/nginx.conf, run the following command:

grep nginx /etc/nginx/nginx.conf

You should see the following output:

pid /run/nginx.pid;
include /etc/nginx/mime.types;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# nginx-naxsi config
# Uncomment it if you installed nginx-naxsi
#include /etc/nginx/naxsi_core.rules;
# nginx-passenger config
# Uncomment it if you installed nginx-passenger
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript

To search for a string called “nginx” in all the files located inside directory /etc/nginx/, run the following command:

grep nginx /etc/nginx/*

You should see the following output:

grep: /etc/nginx/conf.d: Is a directory
/etc/nginx/fastcgi_params:fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
/etc/nginx/koi-utf:# If you need a full and standard map, use contrib/unicode2nginx/koi-utf
/etc/nginx/naxsi-ui.conf.1.4.1:rules_path = /etc/nginx/naxsi_core.rules
/etc/nginx/nginx.conf:pid /run/nginx.pid;
/etc/nginx/nginx.conf: include /etc/nginx/mime.types;
/etc/nginx/nginx.conf: access_log /var/log/nginx/access.log;
/etc/nginx/nginx.conf: error_log /var/log/nginx/error.log;
/etc/nginx/nginx.conf: # nginx-naxsi config
/etc/nginx/nginx.conf: # Uncomment it if you installed nginx-naxsi
/etc/nginx/nginx.conf: #include /etc/nginx/naxsi_core.rules;
/etc/nginx/nginx.conf: # nginx-passenger config
/etc/nginx/nginx.conf: # Uncomment it if you installed nginx-passenger
/etc/nginx/nginx.conf: include /etc/nginx/conf.d/*.conf;
/etc/nginx/nginx.conf: include /etc/nginx/sites-enabled/*;
/etc/nginx/nginx.conf:# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
grep: /etc/nginx/sites-available: Is a directory
grep: /etc/nginx/sites-enabled: Is a directory
/etc/nginx/win-utf:# use contrib/unicode2nginx/win-utf map instead.

Search All Files in a Specific Directory Recursively

To search for a specific string in all files located inside specific directory recursively, use the following syntax:

grep -r «search-string» «/path-of-the-directory»

For example, find all files that containing string called “ubuntu” in the directory /mnt/grub.d recursively, run the following command:

grep -r ubuntu /mnt/grub.d/

You should see the following output:

/mnt/grub.d/10_linux:ubuntu_recovery=»1″
/mnt/grub.d/10_linux: Ubuntu|Kubuntu)
/mnt/grub.d/10_linux:if [ «$ubuntu_recovery» = 1 ]; then
/mnt/grub.d/10_linux: if ([ «$ubuntu_recovery» = 0 ] || [ x$type != xrecovery ]) && \
/mnt/grub.d/05_debian_theme: Tanglu|Ubuntu|Kubuntu)
/mnt/grub.d/05_debian_theme: Ubuntu|Kubuntu)

In the above output, you should see all the search string with the filename.

If you want to display the only filename, run the grep command with -l option:

grep -rl ubuntu /mnt/grub.d/

You should see only filenames that match with search string:

You can also run the following command to display only filenames:

grep -H -R ubuntu /mnt/grub.d/ | cut -d: -f1

You should see the following output:

/mnt/grub.d/10_linux
/mnt/grub.d/10_linux
/mnt/grub.d/10_linux
/mnt/grub.d/10_linux
/mnt/grub.d/05_debian_theme
/mnt/grub.d/05_debian_theme

If you want to display only search string without filenames, run the grep command as shown below:

grep -rh ubuntu /mnt/grub.d/

You should see the following output:

ubuntu_recovery=»1″
Ubuntu|Kubuntu)
if [ «$ubuntu_recovery» = 1 ]; then
if ([ «$ubuntu_recovery» = 0 ] || [ x$type != xrecovery ]) && \
Tanglu|Ubuntu|Kubuntu)
Ubuntu|Kubuntu)

To ignore case when searching for a string, use grep command with -i option as shown below:

grep -ir ubuntu /mnt/grub.d/

You should see the following output:

/mnt/grub.d/10_linux:ubuntu_recovery=»1″
/mnt/grub.d/10_linux: Ubuntu|Kubuntu)
/mnt/grub.d/10_linux:if [ «$ubuntu_recovery» = 1 ]; then
/mnt/grub.d/10_linux: if ([ «$ubuntu_recovery» = 0 ] || [ x$type != xrecovery ]) && \
/mnt/grub.d/05_debian_theme: Tanglu|Ubuntu|Kubuntu)
/mnt/grub.d/05_debian_theme: # Set a monochromatic theme for Tanglu/Ubuntu.
/mnt/grub.d/05_debian_theme: Ubuntu|Kubuntu)

To Find Whole Words Only

To find all the lines that matches only whole words using the following syntax:

grep -wr «word» «/path-of-the-directory»

For example, find all the lines that matches for word “Ubuntu” in /mnt/grub.d directory.

grep -wr Ubuntu /mnt/grub.d/

You should see the following output:

/mnt/grub.d/10_linux: Ubuntu|Kubuntu)
/mnt/grub.d/05_debian_theme: Tanglu|Ubuntu|Kubuntu)
/mnt/grub.d/05_debian_theme: # Set a monochromatic theme for Tanglu/Ubuntu.
/mnt/grub.d/05_debian_theme: Ubuntu|Kubuntu)

If you want to search for two words “Ubuntu” and “Linux” in /mnt/grub.d directory, run the following command:

egrep -wr ‘Ubuntu|Linux’ /mnt/grub.d/

You should see the following output:

/mnt/grub.d/20_linux_xen: OS=GNU/Linux
/mnt/grub.d/20_linux_xen: OS=»$ GNU/Linux»
/mnt/grub.d/20_linux_xen:# the initrds that Linux uses don’t like that.
/mnt/grub.d/20_linux_xen: title=»$(gettext_printf «%s, with Xen %s and Linux %s (%s)» «$» «$» «$» «$(gettext «$«)»)»
/mnt/grub.d/20_linux_xen: title=»$(gettext_printf «%s, with Xen %s and Linux %s» «$» «$» «$«)»
/mnt/grub.d/20_linux_xen: lmessage=»$(gettext_printf «Loading Linux %s . » $
/mnt/grub.d/10_linux: OS=GNU/Linux
/mnt/grub.d/10_linux: Ubuntu|Kubuntu)
/mnt/grub.d/10_linux: OS=»$ GNU/Linux»
/mnt/grub.d/10_linux:# the initrds that Linux uses don’t like that.
/mnt/grub.d/10_linux: title=»$(gettext_printf «%s, with Linux %s (%s)» «$» «$» «$(gettext «$«)»)» ;;
/mnt/grub.d/10_linux: title=»$(gettext_printf «%s, with Linux %s» «$» «$«)» ;;
/mnt/grub.d/10_linux: if [ x»$title» = x»$GRUB_ACTUAL_DEFAULT» ] || [ x»Previous Linux versions>$title» = x»$GRUB_ACTUAL_DEFAULT» ]; then
/mnt/grub.d/10_linux: message=»$(gettext_printf «Loading Linux %s . » $
/mnt/grub.d/30_os-prober: if [ x»$title» = x»$GRUB_ACTUAL_DEFAULT» ] || [ x»Previous Linux versions>$title» = x»$GRUB_ACTUAL_DEFAULT» ]; then
/mnt/grub.d/05_debian_theme: Tanglu|Ubuntu|Kubuntu)
/mnt/grub.d/05_debian_theme: # Set a monochromatic theme for Tanglu/Ubuntu.
/mnt/grub.d/05_debian_theme: Ubuntu|Kubuntu)

To display line numbers with matching words “Ubuntu”, use the grep command with -n option as shown below:

grep -wrn Ubuntu /mnt/grub.d/

You should see the following output:

/mnt/grub.d/10_linux:40: Ubuntu|Kubuntu)
/mnt/grub.d/05_debian_theme:32: Tanglu|Ubuntu|Kubuntu)
/mnt/grub.d/05_debian_theme:33: # Set a monochromatic theme for Tanglu/Ubuntu.
/mnt/grub.d/05_debian_theme:177: Ubuntu|Kubuntu)

Find all Lines that Starting with Lowercase or Uppercase Letter

To search for lines that start with an uppercase letter, use the following syntax:

grep «^[A-Z]» -rns «/path-of-the-directory»

To search for lines that start with a lowercase letter, use the following syntax:

grep «^[a-z]» -rns «/path-of-the-directory»

To search for lines that start with lowercase and uppercase letter, use the following syntax:

grep «^[a-zA-Z]» -rns «/path-of-the-directory»

Conclusion

Thats it for now. We hope you have now enough knowledge on how to use grep command to find a file containing a specific text. Feel free to ask any questions if you have any below in the comments.

Источник

Читайте также:  Как посмотреть список жестких дисков linux
Оцените статью