The following lists all text files in the current directory.
file --mime-type * -F$'\t' | awk -F'\t *' '$2 ~/^text\/plain/ { print $1 }'
Note: This assumes that your filenames have neither embedded tabs nor embedded newlines, which is not typically a problem.
file --mime-type * -F$'\t'
determines the file type of each file in the current folder (*
) and prints a two-column list: the filename at hand, followed by a tab (-F'$\t'
), followed by spaces for alignment, followed by the file type expressed as a MIME type.
awk -F'\t *' '$2 ~/^text\/plain/ { print $1 }'
then parses each line into the filename and MIME type (-F'\t *
), tests if the MIME type (field 2,$2
) starts with (^
) string text/plain
and, if so, prints the filename (field 1, $1
).
To process the resulting files in a loop, use while
:
while IFS= read -r textfile; do
# Work with "$textfile"
done < <(file --mime-type * -F$'\t' | awk -F'\t *' '$2 ~/^text\/plain/ { print $1 }')
Note that while you could call file
in a conditional inside a for file in *
loop, the above approach is much more efficient.
For the record, here's how you would use the command in a conditional:
if [[ $(file -b --mime-type "$file") == 'text/plain'* ]]; then ...