2009-10-28 70 views
5

我試圖在當前和所有子文件夾中grep多個擴展名。grep多個擴展名當前和子文件夾

grep -i -r -n 'hello' somepath/*.{php,html} 

這只是grepping當前文件夾,而不是子文件夾。

這樣做的好方法是什麼?

回答

2

其中的一個:

find '(' -name '*.php' -o -name '*.html' ')' -exec grep -i -n hello {} + 
find '(' -name '*.php' -o -name '*.html' ')' -print0 | xargs -0 grep -i -n hello 
9

只使用grep的:

grep -irn --include='*.php' --include='*.html' 'hello' somepath/ 
+0

這2 screenshot codesearch

貌似這個整潔的解決方案。是否有可能將包含模式混合在一起?我搞砸了glob模式,但從來沒有讓它正常工作。 :-( – Christoph 2013-01-12 13:50:45

0

我一直在尋找同樣的,當決定做一個bash腳本,我開始用vim於codesearch和驚喜,我已經這樣做之前!

#!/bin/bash 
context="$3" 
#ln = line number mt = match mc = file 
export GREP_COLORS="sl=32:mc=00;33:ms=05;40;31:ln=" 
if [[ "$context" == "" ]]; then context=5; fi 
grep --color=always -n -a -R -i -C"$context" --exclude='*.mp*'\ 
--exclude='*.avi'\ 
--exclude='*.flv'\ 
--exclude='*.png'\ 
--exclude='*.gif'\ 
--exclude='*.jpg'\ 
--exclude='*.wav'\ 
--exclude='*.rar'\ 
--exclude='*.zip'\ 
--exclude='*.gz'\ 
--exclude='*.sql' "$2" "$1" | less -R 

將此代碼粘貼到一個文件名爲於codesearch並設置chmod命令700或770 我想這可能下一次,我忘

的是更好地在這裏,該腳本將顏色的顯示匹配並圍繞

./codesearch '/full/path' 'string to search' 

上下文和可選的限定圍繞默認上下文線的數目5

./codesearch '/full/path' 'string to search' 3 

我編輯的代碼,並增加了一些吸引眼球

例如./codesearch ./「EVAL」當您啓用了「允許閃爍的文字」,在終端

相關問題