I want to list and sort all the files in a directory that are ASCII text files but don't necessarily have a .txt extension. How can I do this? I'm new to shell scripting.
1 Answer
Using file
command:
#!/bin/bash
for i in * # for every file in folder
do
# if file reports "ASCII text"
if file "$i" | grep --quiet 'ASCII text$'
then
# print filename
echo "$i"
fi
done
One liner:
$ for i in *; do if file "$i" | grep -q 'ASCII text$' ; then echo "$i"; fi; done
.txt
extension? (I assume they can have).file netflix-backtrace.txt
outputsnetflix-backtrace.txt: ASCII text