3

Similar to, but beyond:

find . -iname "somepatter*txt" -print -exec cat {} \;

How might the file name from each file be printed in front of each line with the results from find?

2
find . -iname "somepattern*txt" -exec grep -H '' {} \+

Kind of an abuse of grep, but it works. Has the bonus of spawning the minimal available processes. This assumes you don't want a single line with the filename before each file.

  • Why -H ? The question never mentioned symbolic links. – DarkHeart Oct 6 '15 at 23:22
  • 1
    @DarkHeart "-H, --with-filename Print the file name for each match. This is the default when there is more than one file to search." Probably for the edge case of only one file being passed to grep. – muru Oct 6 '15 at 23:32
  • @muru - ah ok - on my server -H is something different. – DarkHeart Oct 7 '15 at 0:37
  • 1
    @DarkHeart probably not GNU grep. – muru Oct 7 '15 at 0:39
1

Inelegant approach:

find . -iname "somepattern*txt" -exec awk -v a={} '{ print a " " $0 }' {} \;
  • 2
    Less inelegant -exec awk '{print FILENAME " " $0}' {} +. – dave_thompson_085 Oct 7 '15 at 0:01
  • This is what I'd worked on before posting the question. Thanks. – Donnied Oct 7 '15 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.