0

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.

4
  • Just to get it clear: you mean files do not necessarily have a .txt extension? (I assume they can have). Mar 31, 2015 at 20:10
  • Exactly. And I want to print only the file names.
    – Eduard
    Mar 31, 2015 at 20:12
  • This sounds very much like homework. Hint: file netflix-backtrace.txt outputs netflix-backtrace.txt: ASCII text
    – Elder Geek
    Mar 31, 2015 at 20:22
  • I managed to list the files of type ASCII Text but it is something like "a.txt: ASCII Text". Is there any option to get only the file name? Without the "ASCII Text" part?
    – Eduard
    Mar 31, 2015 at 20:24

1 Answer 1

3

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
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.