That of @rozcietrzewiacz is a great solution, but if you still want to stay with text files (as returned by file), you can carefully build an array of file names, then execute your grep command on that array.
I suppose the following:
- in no filename there is a newline (but spaces can be present);
- a
file util that support -0 and -i options;
- GNU sed, or a sed supporting
\x exadecimal char codes.
Here is an example
#!/bin/bash
get_file_list() {
local path="$1"
find "$path" -type f -exec file -0i {} + |
sed -n '/\x00 *text\//s/\x00.*//p'
}
list=()
while IFS= read -r line; do
list+=("$line")
done < <(get_file_list .)
# to choose options and pattern
grep -i pattern "${list[@]}"
The sed command take a sequence of line of text coming from file, composed from a filename, a NUL byte and the mime-type. If in the second part (after the NUL) there is the word text/ then remove that part and only print the filename, otherwise print nothing.