Find sub directories where a string is not present in one of the files

Viewed 58

I have a directory with lot of subdirectories in Linux.

Each subdirectories contains a file called variables_list.txt

Now in each variables_list.txt file I want to check if a string is present or not.

For example:

if the string is like below

export host_name=

I want to find out all the subdirectories where in the variables_list.txt file the string is not present.

I tried like below

grep -r -l "export host_name=" .

This is printing all the file names where the string is present.

I tried like below as well

grep -Hrn "export host_name=" 

This also prints all the file names where the string is present with the line number

I can I achieve what I want

1 Answers

You can pipe grep results to dirname:

grep -v -Z -r -l "export host_name=" .  | xargs -0 dirname

Example:

$ mkdir {1..10}
$ mkdir 'test dir with space'
$ echo export host_name= >> 2/variables_list.txt
$ echo export host_name= > 3/variables_list.txt
$ echo export host_name= > 4/variables_list.txt
$ echo export host_name= > 5/variables_list.txt
$ echo export host_nname= > 'test dir with space'/variables_list.txt
$ echo export hostt_name= > 3/variables_list.txt
$ find . -name variables_list.txt -exec sh -c 'grep "export host_name=" "$1" >/dev/null || echo "$(dirname "$1")"' sh {} \;
./3
./test dir with space
Related