You can just run this command from a top-level directory e.g. You have also learned how to combine grep -l with grep -i and grep -R to recursively search for all files containing some matching text. That's all about the grep command example to print filenames containing matching String. Here is the screenshot of grep command examples to list only filenames with matching String in Linux: If you want to learn more about the Linux directory structure, these free Linux online courses are a good starting point. This option print all the files with a full path containing matching text, as shown below:ītw, the path printed is the relative path from the current directory and not the absolute path from the root or (/). $ grep -iR Intel * 2015 /jan /cpu.txt :Intel i7 is best CPUĢ015 /motherboard.txt :Intel Motherboard is best you should always buy that. When you normally search without grep -l it prints all files with matching text as well as shown in the following example: I often use grep -iRlto print all the files across directories containing some matching text like hostname, IP Address, or some configuration parameter. The grep -i is for case insensitive search. This option is often used with grep -iR, which recursively searches for files containing matching text in sub-directories as well. Suppress normal output instead, print the name of each inputįile from which output would normally have been printed. Replaced by something more appropriate in those locales.Īnd this is the explanation of grep -l command from grep man page: In other locales, the standard input may be If the standard input is searched, a pathname of (standard input) willīe written, in the POSIX locale. Pathnames are written once per file searched. (The letter ell.) Write only the names of files containing selected This means any investment you made in learning Linux commands like grep will be useful for many years to come.Īnyway, let's understand what does -l option on grep command does here is what the grep -l command option does (from UNIX standard): Linux has already survived for more than 40 years, and I am sure it will remain relevant in the coming years as well. Hence, I also suggest you join one of these online Linux command line courses to learn Linux fundamentals in depth. This option of grep only shows filenames that contain matching text.ītw, a good knowledge of essential Linux commands like find, grep, awk, and sed goes a long way in improving your Linux skills as well as improving your productivity. Since many files contain database references, you might get a lot of data on output, so if you are only interested in all the files containing matching text, you can use the grep -l option. For example, when you are searching for some configurations like a Linux or database hostname across all configuration files in your application host, then you just want to see which file has contained those references. Still, sometimes you only want to grep to show just filename and path and not the matching text. This is actually required and needed in most situations. The grep command from Linux is one of the powerful commands to find files containing some text, but when you use grep, it not only print the file name but also the line, which is including the matching text.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |