Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do recursively grep when there is a FIFO in the path

Tags:

grep

shell

fifo

I want to do a recursively grep. So typically I will do the following:

grep pattern -r /some/path

Normally this will work. However, when there a FIFO file resides in the path, grep will stuck there.

johnson@ISD32_54_sles10:~/tmp$ ls -l 1
prw-r--r-- 1 neoli users 0 2012-05-16 17:24 1

Then I call the strace command to identify the problem, I got this.

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

So my problem is how to do a recursively grep when there is a FIFO in the path? Does grep has a command line option which will tell grep to ignore FIFO when specified?

Thanks for your kind help.

like image 793
tianyapiaozi Avatar asked May 16 '12 09:05

tianyapiaozi


2 Answers

From 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.
like image 180
Fredrik Pihl Avatar answered Nov 15 '22 02:11

Fredrik Pihl


One way would be to use find

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

this will only grep for the pattern on regular files (so no symbolic links, pipes, etc.)

like image 35
dwalter Avatar answered Nov 15 '22 01:11

dwalter