I want to loop through thousands of folders all which contain a file called output(foldernumber).txt and check which of these output files DO NOT contain a keyword and have the script write out a list of these output files, so further analysis can be completed on them.

This is what I have so far:

for i in ~pwd ;do
(cd $i/ && grep -L 'keyword' output"$a".txt >> ../list.txt)

Errors given say the output files do not exist and a blank list.txt file is made. Any advice please?

find . -type f -regex ".*/output[0-9]*\.txt" -exec grep -L 'keyword' '{}' +


 find . -type f -regex ".*/output[0-9]*\.txt" -print0 | xargs -0 grep -L 'keyword'
  • Thank you- both lines work in the script, however I have tested this on 100 folders and I am expecting to get 20 file names of output files that don't contain the keyword- but I am only getting back 4. Any help with what may be causing the script to miss out results? – Jyosk Oct 13 '16 at 10:20
  • You can verify the find results if it contains the missing file at all: find . -type f -regex ".*/output[0-9]*.txt" – rudimeier Oct 13 '16 at 10:26
  • Should the literal dot be escaped (\.txt), since it's a regex match rather than a glob? – steeldriver Oct 13 '16 at 12:07

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.