歡迎您光臨本站 註冊首頁

find與grep

←手機掃碼閱讀     火星人 @ 2014-03-26 , reply:0

在當前目錄(包含子目錄)下查找包含"Test"字元串的所有cpp和h文件的shell命令

find . -name "*" -exec grep -i "xxxx" {} \; -print

其中,-i是grep的參數,表示忽略大小寫

在linux 里man find 里有一段說明:

-exec command ;
Execute command; true if 0 status is returned. All following
arguments to find are taken to be arguments to the command until
an argument consisting of `;' is encountered. The string `{}'
is replaced by the current file name being processed everywhere
it occurs in the arguments to the command, not just in arguments
where it is alone, as in some versions of find. Both of these
constructions might need to be escaped (with a `\') or quoted to
protect them from expansion by the shell. The command is exe-
cuted in the starting directory.

意思是說可以在FIND后調用外部命令;後面出現的所有參數都是該命令的參數直到一個由「;」構成的參數為止。字元串「{}」由當前的正在被處理的文件名所代替。「\」是一個轉義符。

例如:find . -name "*.php" -exec grep viewnew {} -H \;

這條命令的意思是,查當前目錄以下包括各個子目錄中所有PHP文件,並用GREP查代裡面含有「viewnew」字樣,並把這個文件名顯示出來。

另外,要注意的是,在使用時,經常遇到 find: missing argument to `-exec'

這個意思並不是說沒有exec參數,而是exec後面的參數不對。這時,應該檢查\;之前有沒有空格之類。

[火星人 ] find與grep已經有489次圍觀

http://coctec.com/docs/linux/show-post-185953.html