2012-05-16 95 views
4

我想做一個遞歸的grep。因此,通常我會執行以下操作:如何在路徑中存在一個FIFO時遞歸地grep

grep pattern -r /some/path 

通常這會起作用。但是,當路徑中存在一個FIFO文件時,grep將停留在那裏。

[email protected]_54_sles10:~/tmp$ ls -l 1 
prw-r--r-- 1 neoli users 0 2012-05-16 17:24 1 

然後我打電話strace命令來確定問題,我得到了這個。

... 
stat64("./1", {st_mode=S_IFIFO|0644, st_size=0, ...}) = 0 
open("./1", O_RDONLY|O_LARGEFILE) = 3 
read(3, <unfinished ...> 

所以我的問題是如何做一個遞歸grep的時候有一個FIFO的路徑? grep有一個命令行選項,它會告訴grep在指定時忽略FIFO嗎?

感謝您的善意幫助。

回答

5

man grep(GNU)

-D ACTION, --devices=ACTION 

    If an input file is a device, FIFO or socket, use ACTION to process it. 
    By default, ACTION is read, which means that devices are read just as if 
    they were ordinary files. If ACTION is skip, devices are silently skipped. 
2

一種方法是使用find

find . -type f -exec grep pattern {} \; 

這將僅適用於常規文件(所以沒有符號鏈接,管道等模式的grep )

+0

這對FreeBSD很有用,它包含舊的GNU grep,即使提供了'-D skip',也不會跳過FIFO。 – WGH