1

I have a parent folder named 'dev', and inside it are all my project folders. The ReadMe files of these projects contain the app type "type: game", for example. What I would like to do is to:

  1. search through all subdirectories of the dev folder to find all the files with *.md" extension

  2. then return the names of those directories which contain a .md files with containing the phrase "game"

I've tried piping find into grep like so:

find -type f -name "*.md" | grep -ril "type: game"

But it just returns the names of files from all subdirectories which contain the phrase "game" in any file.

marti
  • 69
  • 7
  • 2
    You're looking for the [`xargs`](https://man7.org/linux/man-pages/man1/xargs.1.html) command: `find ... | xargs grep ...` – larsks Jun 05 '22 at 19:58
  • 2
    Or just drop the `find` command and run `grep --include '*.md' -ril "type: game"` – larsks Jun 05 '22 at 19:59

1 Answers1

1

find . -type f -name "*.md" -print0 | xargs -0 grep -il "type: game" | sed -e 's/[^\/]*$//'

This finds any files in the current directory and sub-directories with names ending with .md, then greps for files containing the string. We then use sed to trim the filename leaving only the directories containing a file ending in .md with the "type: game" inside.

James Risner
  • 5,451
  • 11
  • 25
  • 47