One of the most mutual chore spell working inward programming projects is finding files containing only about specific text e.g. y'all bring your application deployed inward Linux server as well as y'all are migrating your database from 1 server to another. Now, y'all desire to file all config files as well as scripts which are referencing your onetime database using the hostname or IP address, therefore that y'all tin supersede them alongside an alias. Well, y'all should ever work an alias to connect to the database or whatever other system, but sometimes it happens y'all bring to work hostname. Anyway, how create y'all uncovering all those files containing hostname inward your Linux machine? Well, the grep ascendancy is hither to assist you.
The grep command allows y'all to scan files to uncovering whatever matching text as well as y'all tin work the recursive selection of grep ascendancy to uncovering all files containing a reference to your onetime database hostnames, or specific text inward general.
You tin combine recursive selection alongside ignore instance selection (-i) to uncovering a specific text similar hostname by ignoring instance inward your config files as well as scripts.
This is the ascendancy I often work to search all files inward dissimilar sub-directories for finding reference to a specific text e.g. a hostname which is going to decommission or needs to hold upward migrated:
The -R selection volition recursively search files inward sub-directories starting from the electrical flow directory as well as -i volition search for the text you're render ignoring case.
Btw, if y'all are non familiar alongside basic Linux commands similar grep, find, chmod etc therefore I propose y'all to root become through a primal course of written report like Linux Command Line Basics on Udemy. This volition assist y'all to acquire to a greater extent than inward a brusque time.
Here is an illustration of the recursive search alongside grep ascendancy which prints both file as well as matching text as well as alone file names:
If y'all also desire to search for the whole word, instead of only matching a role of the text, therefore y'all drive add together selection -w, which matches the whole give-and-take like
volition search for exact "-Xms" text inward all files, it is could to uncovering the scripts where y'all are specifying your JVM arguments.
You tin also work 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, the next ascendancy volition alone search for text inward Java files:
You tin fifty-fifty work the regular expression to render for a to a greater extent than sophisticated search similar the next ascendancy volition search for specified text on .c as well as .h files
This ascendancy volition exclude searching all the files ending alongside .svn extension:
As I bring said before, It's non only of import to acquire novel commands but also novel options of the ascendancy y'all are familiar alongside to brand the most of Linux power. If y'all desire to ameliorate your Linux skills I propose y'all become through
2) The grep -w is used for give-and-take search i.e. it volition check to the exact word
3) The grep -i is used for ignoring instance search e..g it volition check to "Java", "JAVA", "javA" as well as other variants.
4) The grep --include is used to search alone for sure enough types of files.
5) The grep --exclude is used to exclude non-interesting files e.g. log files or .svn files.
That's all close how to search for matching text inward all files inward UNIX as well as Linux. This ascendancy is really powerful as well as 1 of the useful tool to uncovering out files referencing straight hostnames instead of aliases. They are really useful for host migrations as well as database migration. You tin fifty-fifty work them spell troubleshooting issues.
Further Learning
Linux Command Line Basics
example)10 Example of tar commands inward Unix (example) 5 Example of kill commands inward Unix as well as Linux (example) VI Editor examples as well as tips for beginners (example) How to work the netstat ascendancy to uncovering which procedure is listening on a port? (example) How does nslookup ascendancy operate inward UNIX? (answer) 10 examples of lsof ascendancy inward Linux? (examples) 10 books to acquire essential commands of Linux or UNIX? (list) How Linux works? What Every SuperUser should know? (book) 10 Examples of cURL ascendancy inward Linux? (examples) How to work the CUT ascendancy inward Linux (tutorial) How to move out from the telnet ascendancy inward UNIX? (example) How to mail HTTP asking to telephone telephone a spider web service from the ascendancy business inward Linux? (example)
Thanks for reading this article therefore far. If y'all similar this article therefore delight portion alongside your friends as well as colleagues. If y'all bring whatever interrogation or feedback therefore delight drib a comment.
P. S. - If y'all are looking only about gratis courses to acquire or ameliorate your Linux skills y'all should depository fiscal establishment check out this listing of Free Linux Courses for Programmers as well as Developers.
The grep command allows y'all to scan files to uncovering whatever matching text as well as y'all tin work the recursive selection of grep ascendancy to uncovering all files containing a reference to your onetime database hostnames, or specific text inward general.
You tin combine recursive selection alongside ignore instance selection (-i) to uncovering a specific text similar hostname by ignoring instance inward your config files as well as scripts.
This is the ascendancy I often work to search all files inward dissimilar sub-directories for finding reference to a specific text e.g. a hostname which is going to decommission or needs to hold upward migrated:
$ grep -iR text *
The -R selection volition recursively search files inward sub-directories starting from the electrical flow directory as well as -i volition search for the text you're render ignoring case.
Btw, if y'all are non familiar alongside basic Linux commands similar grep, find, chmod etc therefore I propose y'all to root become through a primal course of written report like Linux Command Line Basics on Udemy. This volition assist y'all to acquire to a greater extent than inward a brusque time.
The grep ascendancy to uncovering all files containing specific text inward Linux
By default grep volition impress both the matching text every bit good every bit the advert of the file containing matching text, if y'all are only interested inward file names therefore y'all tin also work the grep ascendancy alongside selection -l every bit shown below:$ grep -iRl text *
Here is an illustration of the recursive search alongside grep ascendancy which prints both file as well as matching text as well as alone file names:
If y'all also desire to search for the whole word, instead of only matching a role of the text, therefore y'all drive add together selection -w, which matches the whole give-and-take like
$grep -iRlw '-Xms' *
volition search for exact "-Xms" text inward all files, it is could to uncovering the scripts where y'all are specifying your JVM arguments.
You tin also work 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, the next ascendancy volition alone search for text inward Java files:
$ grep --include=\*.java -iRw text *
You tin fifty-fifty work the regular expression to render for a to a greater extent than sophisticated search similar the next ascendancy volition search for specified text on .c as well as .h files
$ grep --include=\*.{c,h} -iRw text *
This ascendancy volition exclude searching all the files ending alongside .svn extension:
$ grep --exclude=*.svn -iRw '/path/to/file/' -e "pattern"
As I bring said before, It's non only of import to acquire novel commands but also novel options of the ascendancy y'all are familiar alongside to brand the most of Linux power. If y'all desire to ameliorate your Linux skills I propose y'all become through
Important Points to Remember
1) The grep -r or -R selection is used for recursive search2) The grep -w is used for give-and-take search i.e. it volition check to the exact word
3) The grep -i is used for ignoring instance search e..g it volition check to "Java", "JAVA", "javA" as well as other variants.
4) The grep --include is used to search alone for sure enough types of files.
5) The grep --exclude is used to exclude non-interesting files e.g. log files or .svn files.
That's all close how to search for matching text inward all files inward UNIX as well as Linux. This ascendancy is really powerful as well as 1 of the useful tool to uncovering out files referencing straight hostnames instead of aliases. They are really useful for host migrations as well as database migration. You tin fifty-fifty work them spell troubleshooting issues.
Further Learning
Linux Command Line Basics
example)
Thanks for reading this article therefore far. If y'all similar this article therefore delight portion alongside your friends as well as colleagues. If y'all bring whatever interrogation or feedback therefore delight drib a comment.
P. S. - If y'all are looking only about gratis courses to acquire or ameliorate your Linux skills y'all should depository fiscal establishment check out this listing of Free Linux Courses for Programmers as well as Developers.
No comments:
Post a Comment