I have a folder (/test
) that contains several subfolder and files (at various level). Inside /test
, I want to iterate only text files containing a particular string ($string
), ignoring files and entire directories specified in a file ($to_skip
) by path. This "exclusion list" contains both files and folder paths as shown below:
/test/ex1/fileA
/test/bob/ex1/fileB
/test/ex1/subfolder
/test/jerry/ex2
that is, one path per line. What I've done follows, but it didn't worked (the various options are required for other reasons):
grep -nriFlI "$string" "/test" | grep -vFf "$to_skip" > "$out"
The 1st grep
actually give me the correct list of paths (any text files containing $string
), but the 2nd grep
doesn't filter as expected since $out
contains all the items produced by the 1st grep
except the occurrences corresponding the last pattern.
The strange thing is that it works correctly only for the last pattern. For example, if /test
, among the others data, has the following files/folders
/test/jerry/ex2/f1
/test/jerry/ex2/f2
/test/jerry/ex2/foo/f3
/test/jerry/ex2/bar
and /test/jerry/ex2/
is the last pattern specified in $to_skip
, the paths above (and in general any data under /test/jerry/ex2/
) are being excluded correctly!
How can I achieve my target?
Many thanks!