程式扎記: [Linux 常見問題] Grep, but only certain file extensions

標籤

2016年11月3日 星期四

[Linux 常見問題] Grep, but only certain file extensions

Source From Here 
Question
I am working on writing some scripts to Grep certain directories, but these directories contain all sorts of file types. I want to grep just .h and .cpp for now, but maybe a few others in the future.

Can anyone show me how I would now add just specific file extensions?

How-To
Just use the --include parameter, like this:
// -r, --recursive: Read all files under each directory, recursively, following symbolic links only if they are on the command line. 
$ grep -r "testing" test/*
test/test.sh:This is for testing
test/test.txt:This is for testing


// --include=GLOB: Search only files whose base name matches GLOB (using wildcard matching as described under --exclude).
$ grep --include \*.sh -r "testing" test/* // Search file with file extension .sh
test/test.sh:This is for testing

// --exclude=GLOB: Skip files whose base name matches GLOB (using wildcard matching). A file-name glob can use *, ?, and [...] as wildcards, and \ to quote a wildcard or backslash character literally.
$ grep --exclude \*.sh -r "testing" test/* // Search file not with file extension .sh
test/test.txt:This is for testing


沒有留言:

張貼留言

網誌存檔