How to discovery all files containing specific text inwards Linux? Use grep command

One of the most mutual chore piece working inwards programming projects is finding files containing approximately specific text e.g. yous bring your application deployed inwards Linux server as well as yous are migrating your database from i server to another. Now, yous desire to file all config files as well as scripts which are referencing your one-time database using the hostname or IP address, thus that yous tin supervene upon them alongside an alias. Well, yous should e'er exercise an alias to connect to the database or whatever other system, but sometimes it happens yous bring to exercise hostname. Anyway, how create yous detect all those files containing hostname inwards your Linux machine? Well, the grep ascendency is hither to assistance you.

The grep command allows yous to scan files to detect whatever matching text as well as yous tin exercise the recursive choice of grep ascendency to detect all files containing a reference to your one-time database hostnames, or a specific text inwards general.

You tin combine recursive choice alongside ignore instance choice (-i) to detect a specific text e.g. hostname past times ignoring instance inwards your config files as well as scripts. This is the ascendency I often exercise to search all files inwards unlike sub-directories for finding reference to a specific text e.g. a hostname which is going to decommission or needs to last migrated:

$ grep -iR text *

The -R choice volition recursively search files inwards sub-directories starting from the electrical flow directory as well as -i volition search for the text you're render ignoring case.


By default grep volition impress both the matching text equally good equally the advert of the file containing matching text, if yous are only interested inwards file names as well as thus yous tin also exercise the grep ascendency alongside choice -l equally shown below:

$ grep -iRl text *

Here is an instance of the recursive search alongside grep ascendency which prints both file as well as matching text as well as alone file names:

 One of the most mutual chore piece working inwards programming projects is finding files contai How to detect all files containing specific text inwards Linux? Use grep command



If yous also desire to search for the whole word, instead of only matching a purpose of text, as well as thus yous drive add together choice -w, which matches the whole discussion e.g.

$grep -iRlw '-Xms' *

volition search for exact "-Xms" text inwards all files, it is could to detect the scripts where yous are specifying your JVM arguments.

You tin also exercise options similar --exclude, --include, --exclude-dir, as well as --include-dir for to a greater extent than efficient as well as fast searching. For example, next ascendency volition alone search for text inwards Java files:

$ grep --include=\*.java -iRw text *

You tin fifty-fifty exercise regular expression to render for to a greater extent than sophisticated search e.g. next ascendency volition search for specified text on .c as well as .h files

$ grep --include=\*.{c,h} -iRw text *

This ascendency volition exclude searching all the files ending alongside .svn extension:

$ grep --exclude=*.svn -iRw '/path/to/file/' -e "pattern"



Important points to remember

1) The grep -r or -R choice is used for recursive search

2) The grep -w is used for discussion search i.e. it volition gibe to exact word

3) The grep -i is used for ignoring instance search e..g it volition gibe to "Java", "JAVA", "javA" as well as other variants.

4) The grep --include is used to search alone for for certain types of files.

5) The grep --exclude is used to exclude non-interesting files e.g. log files or .svn files.



That's all almost how to search for matching text inwards all files inwards UNIX as well as Linux. This ascendency is rattling powerful as well as i of the useful tool to detect out files referencing straight hostnames instead of aliases. They are rattling useful for host migrations as well as database migration. You tin fifty-fifty exercise them piece troubleshooting issues.


Further Learning
Linux Command Line Interface (CLI) Fundamentals
Linux System Administration Fundamentals
The Linux Command Line: H5N1 Complete Introduction


Other UNIX ascendency tutorials yous may similar to explore
  • 10 instance of networking ascendency inwards Unix (example)
  • 10 Example of tar commands inwards Unix (example)
  • 5 Example of kill commands inwards Unix as well as Linux (example)
  • VI Editor examples as well as tips for beginners (example)
  • How to exercise the netstat ascendency to detect which procedure is listening on a port? (example)
  • How does nslookup ascendency move inwards UNIX? (answer)
  • 10 examples of lsof ascendency inwards Linux? (examples)
  • 10 books to larn essential commands of Linux or UNIX? (list)
  • How Linux works? What Every SuperUser should know? (book)
  • 10 Examples of cURL ascendency inwards Linux? (examples)
  • How to exercise CUT ascendency inwards Linux (tutorial)
  • How to acquire out from the telnet ascendency inwards UNIX? (example)
  • How to post HTTP asking to telephone cry upwards a spider web service from the ascendency problem inwards Linux? (example)

Thanks for reading this article thus far. If yous similar this article as well as thus delight part alongside your friends as well as colleagues. If yous bring whatever enquiry or feedback as well as thus delight driblet a comment.

Subscribe to receive free email updates:

0 Response to "How to discovery all files containing specific text inwards Linux? Use grep command"

Posting Komentar