

Windows find files containing text how to#
You learned how to search and find a file containing a particular text string (words) under Linux using the grep command. $ grep -E 'word1|word2' -rnw /home/vivek/backups/ Summing up $ grep -i -r 'income tax' ~/accounting/ How do I find all files containing specific text on Linux? Our final example ignore case distinctions in both the search PATTERN and the input files: Pass the -color option to the grep command display matched text/words in color on the terminal:įig.01: grep command in action with colors and hiding the warnings on screen Task: Ignore case $ grep -w -R 'getMyData()' ~/projects/ 2>/dev/null Task: Display matched text in color This will send and hide unwanted output to /dev/null device: To hide all errors or warning message spam generated by the grep command, append 2>/dev/null to grep command. Grep command generate error message as follows due to permission and other issues: $ grep -E -w -R 'word1|word2' ~/projects/ Task: Hide warning spam $ grep -w -R 'getMyData()' ~/projects/ Task: Search for two or more words In this example, search for word ‘getMyData()’ only in ~/projects/ dirctory: You can select only those lines containing matches that form whole words using the -w option. $ grep -h -R 'main()' ~/projects/*.c Task: Display only words You can pass the -h option to suppress inclusion of the file names in the output:

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. To just display the filename use the cut command as follows: Sample outputs: filename.txt: redeem reward You can pass -H option to print the filename for each match: $ sudo grep -R "barfoo" /etc/ Task: Only display filenamesīy default, the grep command prints the matching lines.

Hence, it is better to restrict the search to particular directory as per your needs: The above command may take a lot of time. In other words, use the following command to search for a word called “barfoo”: I want to search the whole Linux server for a string. Trying to find all files containing specific text on my Linux desktop Look for all files containing cacheRoot text on Linux: You can search for a text string all files under each directory, recursively with -r option: $ grep "redeem reward" ~/*.txt Task: Search all subdirectories recursively to find text in files Let us find text called “redeem reward” in files under Linux: In this example, search for a string called ‘redeem reward’ in all text (*.txt) files located in /home/tom/ directory, use: How to search and find all files for a given text string Let us see some common example on how to use grep to search for strings in files. Grep -E -w -R " word-1|word-2" directory-path Grep -E -R " word-1|word-2" /path/to/directory Grep -r -H " text string to search" directory-path Grep -r " text string to search" /directory-path Grep " text string to search" directory-path The Linux syntax to find string in files is as follows: Grep command syntax for finding a file containing a particular text string
