Grep Command – Find All .tex Files in Directories Recursively

findgrepsymlink

I want to expand the recursive search of symlinks to include only .tex files find . -type l -name "Math*" -exec grep -d "recurse" word {} +. Failed pseudocode 1

find . -type l -name "Math*" \
  -exec WHERE CHOOSE ONLY .tex files and Directories \
  -exec grep -d "recurse" word {} +

I cannot type a command for choosing .tex files and directories.
In words, pseudocode 2

  1. Find all symlinks with name "Math" which point to directories.
  2. Recurse all symlinked directories (I think grep is limiting here possibly)
  3. Do basic grep word in file list

How can you do the step (2)?

Best Answer

I think this must be one of the silliest command piplines I ever have concocted:

$ find . -type l -name "Math*" -print0 |
  xargs -0 -n 1 -IXXX find XXX/ -type f -name "*.tex" -print0 |
  xargs -0 fgrep "word"
  1. Find all symbolic links called Math*.
  2. Do find again on each found path, looking for *.tex files. The xargs need to use -n 1 to call find with no more than one pathname. The pathname will be put into the XXX placeholder.
  3. Call fgrep (i.e. grep -F since we have a fixed search string) with the string on found files.
Related Question