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


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...