We have a process that is not running correctly. It is supposed to only run on files with patterns like "AAA*.x12". However, it is also running on files similar to "BBB*.x12" Each directory only contains an AAA type type or BBB type file. While there is an output.log that contains the data I am looking for, it does not contain the name of the file being processed.

Question: I want to grep a string in a filename in a large number of directories. However, I only want to look at directories that contain a second file matching a specific pattern.

In other words, how can I grep the output.log files only in directories that contain files starting with BBB, and ignore the output.log files in directories that contain files starting with AAA

Note: The directory names are sequential numbers and cannot be used to determine which type files they contain


Quick pseudo code, without trying to make an actual script -- want to make sure I understand the questions. Also, assume well-behaved file names (if necessary, robustify the final script to handle file names with control characters, etc).

# get output.log files in directories containing BBB file and search output.log
find /start -name 'BBB*' -type f -print |\
while read f; do echo $(dirname $f)/output.log; done |\
xargs grep search-string

Does that get you on the right track?

  • Perfect, this is exactly what i need to do – Noah Feb 23 '15 at 16:58

Because I'm still a little unclear about the nature of the question, this is my punt.

if a directory contains either AAA.x12 or BBB.x12, but not both and you want to recursively grep only files named output.log within directories containing BBB.x12

find -type f -name 'BBB.x12' -printf '%h\n' | grep -d recurse -sinI string --include="output.log"

  • didn't work for me. I should have included my system type, solaris did not support the 'printf' flag for find; – Noah Feb 23 '15 at 17:08
  • @Noah, find -type f -name 'BBB.x12' -print | xargs ls -d | grep -d recurse -sinI string --include="output.log" – Nodak Feb 23 '15 at 18:16

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.