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.

  • 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


Using file command:

for i in * # for every file in folder
  # if file reports "ASCII text"
  if file "$i" | grep --quiet 'ASCII text$'
      # print filename
      echo "$i"

One liner:

$ for i in *; do if file "$i" | grep -q 'ASCII text$' ; then echo "$i"; fi; done

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.