How to Use grep Recursively Within Certain File Extensions

[ad_1]

Bash Shell

grep is a great tool for searching through files and standard input in Linux and is able to match string and Regex patterns. However, sometimes it’s necessary to control what kinds of files grep searches for, and it has flags built in to do just that.

Only Including Certain Files in grep Searches

By default, grep will search all files in a given folder and its subfolders if you invoke it with the recursive -r flag. This will pick up everything, but if you only want certain extensions, the option you’ll want to use is --include.

The --include flag tells grep to only include files matching a certain pattern. If it’s specified, grep will treat all include flags as a whitelist. You can use this with any Linux glob characters, such as wildcards to match everything including a certain extension:

grep -inr --include \*.txt "foo" ~/folder

Note that this is escaped with a forward slash \ because it’s possible for filenames to have asterisks in them. You can also specify multiple --include flags, for example, searching all HTML, JS, and CSS source files in a wwwroot:

grep -inr --include \*.html --include \*.css --include \*.js "foo" ~/folder

You can similarly also exclude certain file names, which will still match everything except for the glob, acting as a blacklist on top of the existing configuration:

grep -inr --exclude \*.txt "foo" ~/folder

There’s also a flag to exclude entire directories at once:

grep -inr --exclude-dir config "foo" ~/folder

Using find Instead

Alternatively, if you prefer using the find utility to search through files, you can connect it to grep using pipes and xargsfind can do searching with patterns and Regex, and has a number of advantages, including being able to filter files easily based on metadata like size, date created and modified, and other Linux identifiers.

The command is a little obtuse, as you’ll need to use -print0 at the end of find to print out a single line list, and then pass it to xargs -0 and grep from there.

 find ./ -type f -iname "*.txt" -print0 | xargs -0 grep "foo"



[ad_2]
Source : https://www.howtogeek.com/devops/how-to-use-grep-recursively-within-certain-file-extensions/

Leave a Comment

erotica x videos dorporn.com tamil sex mms telugupussy palimas.mobi xnxpunjabi سكس في جيم arabsexeporn.net سكس خلفى عربى gma voltes v cast teleseryehot.com a2z channel 11 山口理紅 javmobile.mobi rebdb-346
hentai artist cg hentaiact.com copipe manga سكس جامد موت hqtube.pro سكس بنات صغر richard yap family hdteleserye.com gen youtube downloader سكس مصرى فلاحه sexarabporn.net نيك كويتى hitozuma life: one time gal 2 madhentai.net hellabunna hentai
bazaar full movie chupaporn.com hind xxx vido 巨乳マニア javlibrary.pro 西野なこ جوني سنس pornovuku.info اكل كس fucking pornstar bukaporn.com marathi bhabhi sex xxx movies gonzo barzoon.info dj punjab