Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print file names in find despite processing the result and grep

Tags:

linux

bash

I have a directory with files to test, say file A, B and C.

To make things easy, let's assume I have a command I want to issue on each of these files and find the one that gives me a proper output.

I will need a pipe myCommand fileName | grep ExpectedResult (in my real case I was looking for a symbol in a library, so it was readelf -s | grep MySymbol).

I want to issue this command on a result of find command.

I find my result with

find . -name *.so -print0 | xargs -0 myCommand | grep ExpectedResult

This works ok, prints ExpectedResult.

What I want to receive is (assuming that the file I look for is B):

A
B
ExpectedResult
C

This way I could see in which file the result has been found. If I was just about to grep the content of the file, I would need a -print switch in find. Unfortunately, if I need piped commands, this would not do.

Obviously, grep -H wouldn't do either, because it will just say (standard input).

How can I override "outgrepping" the file names? Print it on stderr somehow?

I realize I could save my file name in a variable, process it etc, but I'd love to see a simpler approach.

like image 208
Piotr Zierhoffer Avatar asked Sep 19 '13 11:09

Piotr Zierhoffer


2 Answers

One simple way would be to say:

find . -type f -name "*.so" -exec sh -c "echo {} && readelf -s {} | grep mysymbol" \;
like image 185
devnull Avatar answered Sep 27 '22 16:09

devnull


I believe you want something like this:

find . -name *.so -print0 | xargs -0 -I % sh -c 'echo % ; myCommand "%" | grep ExpectedResult'
like image 37
diogovk Avatar answered Sep 27 '22 17:09

diogovk