Showing posts with label grep. Show all posts
Showing posts with label grep. Show all posts

Saturday, November 9, 2019

Grep Ascendency Representative To Listing Exclusively Filenames Amongst Matching String Inwards Linux

The grep ascendance from Linux is ane of the powerful commands to find files containing unopen to text, but when you lot role grep it non solely impress the file advert but also the trace of piece of job which is containing the matching text. This is ok for most of the situations but sometimes you lot solely desire to grep to demo filename too trace of piece of job too non the matching text e.g. when you lot are searching for unopen to configuration e.g. database hostname across all configuration files inward your application host. Since many files comprise the database references, it's possible that you lot mightiness acquire a lot of information on output, thence if you lot are solely interested inward all the files containing matching text, you lot tin role grep -l option. This selection of grep solely shows filenames which comprise matching text.


Here is what the grep -l ascendance option does (from UNIX standard):

-l
(The missive of the alphabet ell.) Write solely the names of files containing selected
lines to criterion output. Pathnames are written in ane trial per file searched.
If the criterion input is searched, a pathname of (standard input) will
be written, inward the POSIX locale. In other locales, the criterion input may be
replaced past times something to a greater extent than appropriate inward those locales.

too this is the explanation of grep -l ascendance from grep human page:

-l, --files-with-matches
Suppress normal output; instead, impress the advert of each input
file from which output would unremarkably convey been printed. The
scanning volition halt on the starting fourth dimension match. (-l is specified by
POSIX.)


This selection is oftentimes used amongst grep -iR, which recursively search for files containing matching text inward sub-directories every bit well. The grep -i is for instance insensitive search. I oftentimes role grep -iRl to impress all the files across directories containing unopen to matching text e.g. hostname or unopen to configuration parameter.

When you lot unremarkably search without grep -l it prints all files amongst matching text every bit good every bit shown inward the next example:


$ grep -iR Intel * 2015/jan/cpu.txt:Intel i7 is best CPU 2015/motherboard.txt:Intel Motherboard is best you lot should ever purchase that. hardware.txt:Intel Corei7

This selection impress all the files amongst a sum path containing matching text every bit shown below:

$ grep -iRl Intel *   2015/jan/cpu.txt   2015/motherboard.txt   hardware.txt


Btw, the path printed is the relative path from electrical flow directory too non the absolute path from the root or (/).


Here is the screenshot of grep ascendance examples to listing solely filenames amongst matching String inward Linux:

 The grep ascendance from Linux is ane of the powerful commands to  Grep Command illustration to List solely Filenames amongst Matching String inward Linux














That's all most the grep ascendance illustration to impress filenames containing matching String. You convey also learned how to combine grep -l amongst grep -i too grep -R to recursively search for all files containing unopen to matching text. You tin only run this ascendance from a exceed score directory e.g. domicile or / directory or may move a top-level directory of your application e.g. /home/appuser/app directory. It's ane of the powerful tools to honor out dependencies spell migrating from ane host to unopen to other host.

Further Learning
Linux Command Line Basics
10 Example of gyre ascendance inward Linux
How to honor all procedure listening on a port?
10 Examples of lsof ascendance inward Linux

Thanks a lot for reading this far. If you lot similar this illustration of grep ascendance to demo solely filenames without matching text too thence delight percentage amongst your friends too colleagues. If you lot convey whatever interrogation or feedback too thence delight drib a note.

Friday, November 8, 2019

Grep Ascendence Example Inward Linux - How To Search All Files Matching Specific Text

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:

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

 One of the most mutual chore spell working inward programming projects is finding files contai GREP Command Example inward Linux - How to Search All Files Matching Specific Text



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 search

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.