I am in a folder with lots of .txt files, I would like to find all the files which contain stringA but don't contain stringB (they are not necessarily in the same line). Does anyone know how to do this?
Find files containing one string but not the other
files;grep;find
As long as your filenames do not contain spaces, tabs, newline or wildcard characters, and if your grep supports the -L option, you can do it as follows:$ cat file1stringAstringC$ cat file2stringAstringB$ grep -L stringB $(grep -l stringA file?)file1The grep executed in the subshell $(), will print all filenames which contain stringA. This filelist is input for the main grep command, which lists all files that do not contain stringB.From man grep  -v, --invert-match          Invert the sense of matching, to select non-matching lines.  (-v is specified by POSIX.)  -L, --files-without-match          Suppress normal output; instead print the name of each input file from which no output would normally have been printed.  The scanning will stop on the first match.  -l, --files-with-matches          Suppress normal output; instead print the name of each input file from which output would normally have been printed.  The scanning will stop on the first match.  (-l is specified by POSIX.)