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


沒有留言:

張貼留言

[ Java 文章收集 ] 局部敏感哈希 (Locality-Sensitive Hashing, LSH) 方法介紹

Source From  Here   Preface   本文主要介紹一種用於海量高維數據的近似最近鄰快速查找技術—— 局部敏感哈希  ( Locality-Sensitive Hashing, LSH ),內容包括了 LSH 的原理、LSH 哈希函數集、以及 LSH 的一...