Linux search string in all files

Содержание
  1. Linux / UNIX Recursively Search All Files For A String
  2. How to use grep command to recursively search All files for a String
  3. Following symtlinks
  4. Case sensitive recursive search
  5. Displaying files name when searching for a string/word
  6. Using find command to search recursively
  7. Finding all files containing specific text on Linux
  8. How to search only files that have specific extensions
  9. Understanding grep command options that used for searching text files
  10. Summing up
  11. Finding a File Containing a Particular Text String In Linux Server
  12. grep command syntax for finding a file containing a particular text string
  13. How to search and find all files for a given text string
  14. Task: Search all subdirectories recursively
  15. Task: Only display filenames
  16. Task: Suppress file names
  17. Task: Display only words
  18. Task: Search for two or more words
  19. Task: Hide warning spam
  20. Task: Display matched text in color
  21. Task: Ignore case
  22. How do I find all files containing specific text on Linux?
  23. Finding text strings within files using grep
  24. How to find all files containing specific text on Linux
  25. Syntax
  26. grep -rwl “search-string” /path/to/serch/dir
  27. 1. Search Single String in All Files
  28. 2. Search Multiple String in All Files
  29. 3. Search String in Specific Files
  30. 4. Exclude Some Files from Search
  31. 5. Exclude Some Directories from Search
  32. Frequently Uses Command Switches
  33. Linux search string in all files
  34. Find string in file
  35. Find string in file ignoring cases
  36. Find string in current directory
  37. Find string recursively
  38. Find files that do not contain a string
  39. Find string recursively in only some specific files
  40. Find string recursively in all files except the ones that contain certain extensions
  41. Find string recursively all files including some extensions and excluding others
  42. Find string recursively in only some specific files and show their filename
  43. Find files and find a string in them using find
  44. Find Files Containing Specific Text in Linux
  45. Find files containing specific text with mc
  46. About Sergey Tkachenko
  47. 6 thoughts on “ Find Files Containing Specific Text in Linux ”

Linux / UNIX Recursively Search All Files For A String

H ow do I recursively search all text files for a string such as foo under UNIX / Linux / *BSD / Mac OS X shell prompt?

You can use grep command or find command as follows to search all files for a string or words recursively.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux or Unix with grep and find utilities
Est. reading time 2 minutes

The syntax is as follows for the grep command to find all files under Linux or Unix in the current directory:
cd /path/to/dir
grep -r «word» .
grep -r «string» .
The -r option read/sarch all files under each directory, recursively, following symbolic links only if they are on the command line. In other words, it will look into sub-directories too. We can also state path as follows:
grep -r ‘something’ /path/to/dir

The following syntax will read and search all files under each directory, recursively. Follow all symbolic links too by passing the -R (capital R ):
grep -R ‘word’ .
grep -R ‘string-to-search’ /path/to/dir/

To ignore case distinctions, try:
grep -ri «word» .

Displaying files name when searching for a string/word

To display print only the filenames with GNU grep, enter:
grep -r -l «foo» .
You can also specify directory name:
grep -r -l «foo» /path/to/dir/*.c

Using find command to search recursively

find command is recommend because of speed and ability to deal with filenames that contain spaces.

  • 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

Older UNIX version should use xargs to speed up things:
find /path/to/dir -type f | xargs grep -l «foo»
It is good idea to pass -print0 option to find command that it can deal with filenames that contain spaces or other metacharacters:
find /path/to/dir -type f -print0 | xargs -0 grep -l «foo»
OR use the following OSX/BSD/find or GNU/find example:

Sample outputs from the last command:

Fig.01: Unix and Linux: How to Grep Recursively?

Finding all files containing specific text on Linux

Say you want to find orange and mango words, then try:
grep -r -E ‘orange|mango’ .
grep -r -E ‘orange|mango’ /dir/to/search/
This is how you set up pattern
grep -r -e ‘pattern’ /dir/to/search
For extended grep (see egrep command for regular expressions):
egrep -r ‘word’ /dir/to/search/
egrep -r ‘regex’ /dir/to/search/
We can combine all options too:
grep -rnw -e ‘pattern’ /dir/to/search/
egrep -rnw ‘regex’ /path/to/search/

How to search only files that have specific extensions

Want to search files having either ‘.pl’ or ‘.php’ extensions for foo() ? Try:
grep —include=\*. -rnw «foo()» /dir/to/search/
egrep —include=\*. -rnw «regex» /dir/to/search/
We can skip any command-line file with a name suffix that matches the pattern GLOB, using wildcard matching. For instance, exclude all .bin files:
grep —exclude=\*.bin -r -n -0 ‘string_to_search’ /path/
egrep —exclude=\*.bin -r -n -0 ‘regex’ /path/to/search/
When searching recursively, we can skip any subdirectory whose base name matches wildcard. For instance, skip includes and docs directory:

Understanding grep command options that used for searching text files

  • -r : Rrecursive search
  • -i : Ignore case distinctions in patterns and data
  • -w : Match only whole words
  • -n : Show line number with output lines
  • -e ‘pattern’ : Use PATTERNS for matching
  • -E : All search PATTERNS are extended regular expressions
  • —include=GLOB : Search only files that match GLOB (a file pattern)
  • —exclude=GLOB : Skip files that match GLOB
  • —exclude-dir=GLOB : Skip directories that match GLOB

GLOB means to expand to wildcard patterns. For example, GLOB, *.txt means all files ending with .txt extension. A string is a wildcard pattern if it contains one of the following characters:

  1. ? – Matches any single character.
  2. * – Matches any string, including the empty string.
  3. [

Globbing is the operation that expands a wildcard pattern into the list of path-names matching the pattern.

Summing up

You learned how to search for text, string, or words recursively on Linux, macOS, *BSD, and Unix-like systems. See the following man pages:
man grep
man find
man 3 glob
man 7 glob

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

Источник

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

Источник

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.

Источник

Linux search string in all files

The commands used are mainly grep and find.

Find string in file

grep string filename

grep name file.txt

Find string in file ignoring cases

grep string filename

grep -i name file.txt

Find string in current directory

grep string .

Find string recursively

grep -r string .

Find files that do not contain a string

grep -L string .

Find string recursively in only some specific files

grep string -r . —include=*.myextension

grep string -r . —include=*.

grep «name=Oscar» -r . —include=*.js

* if you specify —include it won’t look for the string in all files, just the ones included

Find string recursively in all files except the ones that contain certain extensions

grep string -r . —exclude=*.

grep «Serializable» -rl . —exclude=*.

Find string recursively all files including some extensions and excluding others

grep string -r . —include=*.myextension —exclude=*.myextension2

grep «my=string» -r . —include=*. —exclude=*.js

*It won’t look for the string in the js files.

Find string recursively in only some specific files and show their filename

grep string -rl . —include=*.myextension

grep «name=Oscar» -rl . —include=*.js

Find files and find a string in them using find

find . -name ‘*.extension’ -exec grep string +

find . -name ‘*.txt’ -exec grep Mytext <> +

find . -type f \( -name ‘*.htm’ -or -name ‘*.html’ \) -exec grep -i «mystring» <> +

Источник

Find Files Containing Specific Text in Linux

Linux, regardless of the distro you use, comes with a number of GUI tools which allow searching for files. Many modern file managers support file searching right in the file list. However, most of them do not allow you to search inside a file’s contents. Here are two methods you can use to search for file contents in Linux.

I would like to share the methods I use myself.
The first method involves the grep utility, which exists in any distro, even in embedded systems built on busybox.

To find files containing specific text in Linux, do the following.

  1. Open your favorite terminal app. XFCE4 terminal is my personal preference.
  2. Navigate (if required) to the folder in which you are going to search files with some specific text.
  3. Type the following command:

Here are the switches:
-i — ignore text case
-R — recursively search files in subdirectories.
-l — show file names instead of file contents portions.

./ — the last parameter is the path to the folder containing files you need to search for your text. In our case, it is the current folder with the file mask. You can change it to the full path of the folder. For example, here is my command

Note: Other useful switches you might want to use with grep:
-n — show the line number.
-w — match the whole word.

Another method I use is Midnight Commander (mc), the console file manager app. Unlike grep, mc is not included by default in all Linux distros I’ve tried. You may need to install it yourself.

Find files containing specific text with mc

To find files containing some specific text using Midnight Commander, start the app and press the following sequence on the keyboard:
Alt + Shift + ?
This will open the search dialog.

Fill in the «Content:» section and press the Enter key. It will find all files with the required text.

You can place these files in the left or right panel using the Panelize option and copy/move/delete/view/do whatever you want them.

Midnight Commander is a very time-saving tool when it comes to search.

Winaero greatly relies on your support. You can help the site keep bringing you interesting and useful content and software by using these options:

Share this post

About Sergey Tkachenko

Sergey Tkachenko is a software developer from Russia who started Winaero back in 2011. On this blog, Sergey is writing about everything connected to Microsoft, Windows and popular software. Follow him on Telegram, Twitter, and YouTube.

6 thoughts on “ Find Files Containing Specific Text in Linux ”

The code that you provided helped me. There are also another commands which I cannot remember to find text in files but this one is made it quickly. I have bookmarked this post for further usage. Thank you.

WHAT ABOUT WINDOWS?!

I use Total Commander for that.

Midnight Commander reminds me of XTree for DOS way, evidently, way way, back in the day!! 🙂 Anyone else remember!?

It reminds me of Norton Commander. Good days.

Источник

Читайте также:  Причины постоянной перезагрузки windows
Оцените статью