×
Linux Find File containing Text

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:

man grep

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:

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

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="${GRUB_DISTRIBUTOR} 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)" "${os}" "${xen_version}" "${version}" "$(gettext "${GRUB_RECOVERY_TITLE}")")"
/mnt/grub.d/20_linux_xen: title="$(gettext_printf "%s, with Xen %s and Linux %s" "${os}" "${xen_version}" "${version}")"
/mnt/grub.d/20_linux_xen: lmessage="$(gettext_printf "Loading Linux %s ..." ${version})"
/mnt/grub.d/10_linux: OS=GNU/Linux
/mnt/grub.d/10_linux: Ubuntu|Kubuntu)
/mnt/grub.d/10_linux: OS="${GRUB_DISTRIBUTOR} 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)" "${os}" "${version}" "$(gettext "${GRUB_RECOVERY_TITLE}")")" ;;
/mnt/grub.d/10_linux: title="$(gettext_printf "%s, with Linux %s" "${os}" "${version}")" ;;
/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 ..." ${version})"
/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.

Comments & Discussion:

  1. Is this close to the proper way to do it? If not, how should I? This ability to find text strings in files would be extraordinarily useful for some programming projects I’m doing.

Comments are closed.