linuxshellgrepfind

find command output filtered with grep


I am using a Linux command to find all files with the .txt and .cmd extensions inside a root folder, excluding files located in subdirectories named "Sub Processes" and "SubProcesses." The current command I use is:

find . \( -name '*.txt' -o -name '*.cmd' \) ! \( -path '*/Sub Processes/*' -o -path '*/SubProcesses/*' \)

Now, I want to refine this command so that only the .txt files that contain INSIDE THEM the string "starter" are displayed in the output, while still including all .cmd files as is.

I attempted to find a solution online and through AI engines but haven't been successful. Is there a way to apply the -exec grep 'starter' command only to the .txt files, ensuring they are the only ones checked for this string(inside the file itself) in the output?

Note: I dont want to open a new Bash or Shell interpreter while executing the command.


Solution

  • You can use grep in the find command :

    find . ! \( -path '*/Sub Processes/*' -o -path '*/SubProcesses/*' \) \( -name '*.cmd' -o -name '*.txt' -exec grep -q starter {} \; \) -print