Linux find string in folder

How to search for strings inside files in a folder?

Is there any utility to make searches for a string inside ASCII files to avoid command line searches? How to make a command line search, for example for the string «test» inside all files in the directory /var/x/ ?

9 Answers 9

I assume that your first question is about a GUI alternative to the grep command. I can’t help you with that, I always find the command line very effective.

As for the command line, try

If you want to search recursively (i.e. not only in /var/x/ , but also in subdirectories thereof), do

To avoid grepping the files which grep thinks to be binary, use the -I option:

If grep thinks a file is binary (based on first few bytes of the file), it will assume it does not match instead of going through the whole file.

Well, grep will also try search for the string in a binary file, and report it if it matches: Binary file file.jpg matches

@January When a binary file is read as text, it often has extremely long «lines» (because a character or character sequence that would be interpreted to designate the end of a line may not appear for a long time, or ever). Depending on the way a text search utility is implemented, this could cause performance problems if each «line» is read fully into memory and then checked to see if it matches the search string (which is a reasonable way for grep to be coded, though I don’t know if Ubuntu’s grep is written that way).

Источник

How to Find a Specific String or Word in Files and Directories

Do you want to find all files that contain a particular word or string of text on your entire Linux system or a given directory. This article will guide you on how to do that, you will learn how to recursively dig through directories to find and list all files that contain a given string of text.

A simple way to work this out is by using grep pattern searching tool, is a powerful, efficient, reliable and most popular command-line utility for finding patterns and words from files or directories on Unix-like systems.

The command below will list all files containing a line with the text “check_root”, by recursively and aggressively searching the ~/bin directory.

Читайте также:  Как установить питон 3 на линукс

Find a Word in Directory

Where the -R option tells grep to read all files under each directory, recursively, following symbolic links only if they are on the command line and option -w instructs it to select only those lines containing matches that form whole words, and -e is used to specify the string (pattern) to be searched.

You should use the sudo command when searching certain directories or files that require root permissions (unless you are managing your system with the root account).

$ sudo grep -Rw / -e 'check_root'

To ignore case distinctions employ the -i option as shown:

$ grep -Riw ~/bin/ -e 'check_root'

If you want to know the exact line where the string of text exist, include the -n option.

$ grep -Rinw ~/bin/ -e 'check_root'

Find String with Line Number

Assuming there are several types of files in a directory you wish to search in, you can also specify the type of files to be searched for instance, by their extension using the —include option.

This example instructs grep to only look through all .sh files.

$ grep -Rnw --include=\*.sh ~/bin/ -e 'check_root'

In addition, it is possible to search for more than one pattern, using the following command.

$ grep -Rinw ~/bin/ -e 'check_root' -e 'netstat'

Find Multiple Words in Files

That’s It! If you know any other command-line trick to find string or word in files, do share with us or ask any questions regarding this topic, use the comment form below.

Источник

Linux Find File containing Text – How to Use GREP to Find Strings of Text in Files, Folders & Sub-directories!

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»

  • -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:

Читайте также:  Linux завершить все сессии

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:

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:

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:

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:

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:

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:

Читайте также:  Linux named conf file

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

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

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:

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:

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.

Recent Posts

  • Forcepoint Next-Gen Firewall Review & Alternatives
  • 7 Best JMX Monitoring Tools
  • Best PostgreSQL Backup Tools
  • Best CSPM Tools
  • Best Cloud Workload Security Platforms
  • Best Automated Browser Testing Tools
  • Event Log Forwarding Guide
  • Best LogMeIn Alternatives
  • Citrix ShareFile Alternatives
  • SQL Server Security Basics
  • Cloud Security Posture Management Guide
  • Cloud Workload Security Guide
  • The Best JBoss Monitoring Tools
  • ITL Guide And Tools
  • 10 Best Enterprise Password Management Solutions

Источник

Оцените статью
Adblock
detector