Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Simplified final script
Source Link
joechip
  • 678
  • 3
  • 6

As I understand it, in general terms you want to list which folders contain regular files called dev/*/* which themselves contain the string "MY_Output" (case sensitive).

More precisely, since the "*" pattern does not match files or directories starting with a ".", you would also want to search for dev/*/*, dev/.??*/*, dev/*/.??* and dev/.??*/.??* . This is a detail that is missing from secretmike's and Brian Showalter's solutions.

Using these four patterns ensures that you process all the files you want and that you don't get extra matches from other (deeper or shallower) files. Normally the list of matching files can be accomplished simply by running:

grep -l MY_Output dev/*/* dev/.??*/* dev/*/.??* dev/.??*/.??* 2>/dev/null 

The 2>/dev/null part is there to ignore errors such as when you try reading from files without permissions, like you seem to be doing (based on your answer to Imre L's answer). For best results you may want to run this command as root.

Unfortunately there's a limit to command line lengths, and this command may fail if there are too many matching files because the command line would be too long (after expansion). Since you say there's hundreds of directories under dev, this approach is not appropriate, although I figure it's worth mentioning here for completeness.

To avoid that problem, the find command is better suited:

find dev -mindepth 2 -maxdepth 2 -type f \ -exec bash -c 'grep -q MY_Output {} && echo {}' \; 

This is very similar to the answer Imre L gave, although he resorts to xargs instead of using the -exec switch. But that only gets you the list of matching files, not the list of folders that contain those.

To get what you want we need to filter it a bit further:

find dev -mindepth 2 -maxdepth 2 -type f \ -exec bash -c 'grep -q MY_Output {} && echodirname {}' \; \ | awk '{system("dirname \"" $0 "\"")}' \ | sort \ | uniq 

The -mindepth and -maxdepth switches ensure we don't get matches from files deeper or shallower in the dev tree.

As I understand it, in general terms you want to list which folders contain regular files called dev/*/* which themselves contain the string "MY_Output" (case sensitive).

More precisely, since the "*" pattern does not match files or directories starting with a ".", you would also want to search for dev/*/*, dev/.??*/*, dev/*/.??* and dev/.??*/.??* . This is a detail that is missing from secretmike's and Brian Showalter's solutions.

Using these four patterns ensures that you process all the files you want and that you don't get extra matches from other (deeper or shallower) files. Normally the list of matching files can be accomplished simply by running:

grep -l MY_Output dev/*/* dev/.??*/* dev/*/.??* dev/.??*/.??* 2>/dev/null 

The 2>/dev/null part is there to ignore errors such as when you try reading from files without permissions, like you seem to be doing (based on your answer to Imre L's answer). For best results you may want to run this command as root.

Unfortunately there's a limit to command line lengths, and this command may fail if there are too many matching files because the command line would be too long (after expansion). Since you say there's hundreds of directories under dev, this approach is not appropriate, although I figure it's worth mentioning here for completeness.

To avoid that problem, the find command is better suited:

find dev -mindepth 2 -maxdepth 2 -type f \ -exec bash -c 'grep -q MY_Output {} && echo {}' \; 

This is very similar to the answer Imre L gave, although he resorts to xargs instead of using the -exec switch. But that only gets you the list of matching files, not the list of folders that contain those.

To get what you want we need to filter it a bit further:

find dev -mindepth 2 -maxdepth 2 -type f \ -exec bash -c 'grep -q MY_Output {} && echo {}' \; \ | awk '{system("dirname \"" $0 "\"")}' \ | sort \ | uniq 

The -mindepth and -maxdepth switches ensure we don't get matches from files deeper or shallower in the dev tree.

As I understand it, in general terms you want to list which folders contain regular files called dev/*/* which themselves contain the string "MY_Output" (case sensitive).

More precisely, since the "*" pattern does not match files or directories starting with a ".", you would also want to search for dev/*/*, dev/.??*/*, dev/*/.??* and dev/.??*/.??* . This is a detail that is missing from secretmike's and Brian Showalter's solutions.

Using these four patterns ensures that you process all the files you want and that you don't get extra matches from other (deeper or shallower) files. Normally the list of matching files can be accomplished simply by running:

grep -l MY_Output dev/*/* dev/.??*/* dev/*/.??* dev/.??*/.??* 2>/dev/null 

The 2>/dev/null part is there to ignore errors such as when you try reading from files without permissions, like you seem to be doing (based on your answer to Imre L's answer). For best results you may want to run this command as root.

Unfortunately there's a limit to command line lengths, and this command may fail if there are too many matching files because the command line would be too long (after expansion). Since you say there's hundreds of directories under dev, this approach is not appropriate, although I figure it's worth mentioning here for completeness.

To avoid that problem, the find command is better suited:

find dev -mindepth 2 -maxdepth 2 -type f \ -exec bash -c 'grep -q MY_Output {} && echo {}' \; 

This is very similar to the answer Imre L gave, although he resorts to xargs instead of using the -exec switch. But that only gets you the list of matching files, not the list of folders that contain those.

To get what you want we need to filter it a bit further:

find dev -mindepth 2 -maxdepth 2 -type f \ -exec bash -c 'grep -q MY_Output {} && dirname {}' \; \ | sort \ | uniq 

The -mindepth and -maxdepth switches ensure we don't get matches from files deeper or shallower in the dev tree.

More info on avoiding extra matches; added 1 characters in body
Source Link
joechip
  • 678
  • 3
  • 6

As I understand it, in general terms you want to list which folders contain regular files called dev/*/* which themselves contain the string "MY_Output" (case sensitive). 

More precisely, since the "*" pattern does not match files or directories starting with a ".", you would also want to search for dev/*/*, dev/.??*/*, dev/*/.??* and dev/.??*/.??* . This is a detail that is missing from secretmike's and Brian Showalter's solutions.

Using these four patterns ensures that you process all the files you want and that you don't get extra matches from other (deeper or shallower) files. Normally the list of matching files can be accomplished simply by running:

grep -lcl MY_Output dev/*/* dev/.??*/* dev/*/.??* dev/.??*/.??* 2>/dev/null 

The 2>/dev/null part is there to ignore errors such as when you try reading from files without permissions, like you seem to be doing (based on your answer to Imre L's answer). For best results you may want to run this command as root.

Unfortunately there's a limit to command line lengths, and this command may fail if there are too many matching files because the command line would be too long (after expansion). Since you say there's hundreds of directories under dev, this approach is not appropriate, although I figure it's worth to mention itmentioning here for completeness.

To avoid that problem, the find command is better suited:

find dev -mindepth 2 -maxdepth 2 -type f \ -exec bash -c 'grep -q MY_Output {} && echo {}' \; 

This is very similar to the answer Imre L gave, although he resorts to xargs instead of using the -exec switch. But that only gets you the list of matching files, not the list of folders that contain those.

To get what you want we need to filter it a bit further:

find dev -mindepth 2 -maxdepth 2 -type f \ -exec bash -c 'grep -q MY_Output {} && echo {}' \; \ | awk '{system("dirname \"" $0 "\"")}' \ | sort \ | uniq 

The -mindepth and -maxdepth switches ensure we don't get matches from files deeper or shallower in the dev tree.

As I understand it, in general terms you want to list which folders contain regular files called dev/*/* which themselves contain the string "MY_Output" (case sensitive). More precisely, since the "*" pattern does not match files or directories starting with a ".", you would also want to search for dev/*/*, dev/.??*/*, dev/*/.??* and dev/.??*/.??* .

Using these four patterns ensures that you don't get extra matches from other (deeper) files. Normally the list of matching files can be accomplished simply by running:

grep -lc MY_Output dev/*/* dev/.??*/* dev/*/.??* dev/.??*/.??* 2>/dev/null 

The 2>/dev/null part is there to ignore errors such as when you try reading from files without permissions, like you seem to be doing (based on your answer to Imre L's answer). For best results you may want to run this command as root.

Unfortunately there's a limit to command line lengths, and this command may fail if there are too many matching files because the command line would be too long (after expansion). Since you say there's hundreds of directories under dev, this approach is not appropriate, although I figure it's worth to mention it here for completeness.

To avoid that problem, the find command is better suited:

find dev -mindepth 2 -maxdepth 2 -type f \ -exec bash -c 'grep -q MY_Output {} && echo {}' \; 

This is very similar to the answer Imre L gave, although he resorts to xargs instead of using the -exec switch. But that only gets you the list of matching files, not the list of folders that contain those.

To get what you want we need to filter it a bit further:

find dev -mindepth 2 -maxdepth 2 -type f \ -exec bash -c 'grep -q MY_Output {} && echo {}' \; \ | awk '{system("dirname \"" $0 "\"")}' \ | sort \ | uniq 

As I understand it, in general terms you want to list which folders contain regular files called dev/*/* which themselves contain the string "MY_Output" (case sensitive). 

More precisely, since the "*" pattern does not match files or directories starting with a ".", you would also want to search for dev/*/*, dev/.??*/*, dev/*/.??* and dev/.??*/.??* . This is a detail that is missing from secretmike's and Brian Showalter's solutions.

Using these four patterns ensures that you process all the files you want and that you don't get extra matches from other (deeper or shallower) files. Normally the list of matching files can be accomplished simply by running:

grep -l MY_Output dev/*/* dev/.??*/* dev/*/.??* dev/.??*/.??* 2>/dev/null 

The 2>/dev/null part is there to ignore errors such as when you try reading from files without permissions, like you seem to be doing (based on your answer to Imre L's answer). For best results you may want to run this command as root.

Unfortunately there's a limit to command line lengths, and this command may fail if there are too many matching files because the command line would be too long (after expansion). Since you say there's hundreds of directories under dev, this approach is not appropriate, although I figure it's worth mentioning here for completeness.

To avoid that problem, the find command is better suited:

find dev -mindepth 2 -maxdepth 2 -type f \ -exec bash -c 'grep -q MY_Output {} && echo {}' \; 

This is very similar to the answer Imre L gave, although he resorts to xargs instead of using the -exec switch. But that only gets you the list of matching files, not the list of folders that contain those.

To get what you want we need to filter it a bit further:

find dev -mindepth 2 -maxdepth 2 -type f \ -exec bash -c 'grep -q MY_Output {} && echo {}' \; \ | awk '{system("dirname \"" $0 "\"")}' \ | sort \ | uniq 

The -mindepth and -maxdepth switches ensure we don't get matches from files deeper or shallower in the dev tree.

Explain 2>/dev/null
Source Link
joechip
  • 678
  • 3
  • 6

As I understand it, in general terms you want to list which folders contain regular files called dev/*/* which themselves contain the string "MY_Output" (case sensitive). More precisely, since the "*" pattern does not match files or directories starting with a ".", you would also want to search for dev/*/*, dev/.??*/*, dev/*/.??* and dev/.??*/.??* .

Using these four patterns ensures that you don't get extra matches from other (deeper) files. Normally the list of matching files can be accomplished simply by running:

grep -lc MY_Output dev/*/* dev/.??*/* dev/*/.??* dev/.??*/.??* 2>/dev/null 

The 2>/dev/null part is there to ignore errors such as when you try reading from files without permissions, like you seem to be doing (based on your answer to Imre L's answer). For best results you may want to run this command as root.

Unfortunately there's a limit to command line lengths, and this command may fail if there are too many matching files because the command line would be too long (after expansion). Since you say there's hundreds of directories under dev, this approach is not appropriate, although I figure it's worth to mention it here for completeness.

To avoid that problem, the find command is better suited:

find dev -mindepth 2 -maxdepth 2 -type f \ -exec bash -c 'grep -q MY_Output {} && echo {}' \; 

This is very similar to the answer Imre L gave, although he resorts to xargs instead of using the -exec switch. But that only gets you the list of matching files, not the list of folders that contain those.

To get what you want we need to filter it a bit further:

find dev -mindepth 2 -maxdepth 2 -type f \ -exec bash -c 'grep -q MY_Output {} && echo {}' \; \ | awk '{system("dirname \"" $0 "\"")}' \ | sort \ | uniq 

As I understand it, in general terms you want to list which folders contain regular files called dev/*/* which themselves contain the string "MY_Output" (case sensitive). More precisely, since the "*" pattern does not match files or directories starting with a ".", you would also want to search for dev/*/*, dev/.??*/*, dev/*/.??* and dev/.??*/.??* .

Using these four patterns ensures that you don't get extra matches from other (deeper) files. Normally the list of matching files can be accomplished simply by running:

grep -lc MY_Output dev/*/* dev/.??*/* dev/*/.??* dev/.??*/.??* 2>/dev/null 

Unfortunately there's a limit to command line lengths, and this command may fail if there are too many matching files because the command line would be too long (after expansion). Since you say there's hundreds of directories under dev, this approach is not appropriate, although I figure it's worth to mention it here for completeness.

To avoid that problem, the find command is better suited:

find dev -mindepth 2 -maxdepth 2 -type f \ -exec bash -c 'grep -q MY_Output {} && echo {}' \; 

This is very similar to the answer Imre L gave, although he resorts to xargs instead of using the -exec switch. But that only gets you the list of matching files, not the list of folders that contain those.

To get what you want we need to filter it a bit further:

find dev -mindepth 2 -maxdepth 2 -type f \ -exec bash -c 'grep -q MY_Output {} && echo {}' \; \ | awk '{system("dirname \"" $0 "\"")}' \ | sort \ | uniq 

As I understand it, in general terms you want to list which folders contain regular files called dev/*/* which themselves contain the string "MY_Output" (case sensitive). More precisely, since the "*" pattern does not match files or directories starting with a ".", you would also want to search for dev/*/*, dev/.??*/*, dev/*/.??* and dev/.??*/.??* .

Using these four patterns ensures that you don't get extra matches from other (deeper) files. Normally the list of matching files can be accomplished simply by running:

grep -lc MY_Output dev/*/* dev/.??*/* dev/*/.??* dev/.??*/.??* 2>/dev/null 

The 2>/dev/null part is there to ignore errors such as when you try reading from files without permissions, like you seem to be doing (based on your answer to Imre L's answer). For best results you may want to run this command as root.

Unfortunately there's a limit to command line lengths, and this command may fail if there are too many matching files because the command line would be too long (after expansion). Since you say there's hundreds of directories under dev, this approach is not appropriate, although I figure it's worth to mention it here for completeness.

To avoid that problem, the find command is better suited:

find dev -mindepth 2 -maxdepth 2 -type f \ -exec bash -c 'grep -q MY_Output {} && echo {}' \; 

This is very similar to the answer Imre L gave, although he resorts to xargs instead of using the -exec switch. But that only gets you the list of matching files, not the list of folders that contain those.

To get what you want we need to filter it a bit further:

find dev -mindepth 2 -maxdepth 2 -type f \ -exec bash -c 'grep -q MY_Output {} && echo {}' \; \ | awk '{system("dirname \"" $0 "\"")}' \ | sort \ | uniq 
Source Link
joechip
  • 678
  • 3
  • 6
Loading