M: Use the linux file command to delete all text files in a directory.
G: TL;DR: use
file via find, then delete matches.
find . -maxdepth 1 -type f -exec file --mime-type {} + \
| awk -F: '$2 ~ /text\// {print $1}' \
| xargs rm -fWhat this does
find … -type f → only regular files in the
directoryfile --mime-type → classifies by actual content, not
extensiontext/* → matches all text formatsrm -f → deletes themSafer dry-run (recommended first):
find . -maxdepth 1 -type f -exec file --mime-type {} + \
| awk -F: '$2 ~ /text\// {print $1}'Alternative (null-safe, no xargs):
find . -maxdepth 1 -type f -exec sh -c '
for f; do
file --mime-type "$f" | grep -q "text/" && rm -f "$f"
done
' sh {} +日本語(にほんご):
M: Sorry, I meant a directory and all subdirectories. And I'm looking just for the extension txt.
G: TL;DR: recursive delete by extension only:
find . -type f -name '*.txt' -deleteThat deletes all .txt files in the
current directory and all subdirectories.
Dry-run first (recommended):
find . -type f -name '*.txt'If you want case-insensitive (.TXT,
.Txt, etc.):
find . -type f -iname '*.txt' -deleteIf filenames may start with - and you prefer
explicit rm:
find . -type f -iname '*.txt' -exec rm -f -- {} +日本語(にほんご):