How to list all ASCII files present in a directory?

I want to list all ASCII files that are without extensions(.txt) in my present working directory(home). I started with the ls command in the terminal but i don't know what i should put in the options as i want to list files that have no extensions but only a name. How do i do it?


Solution 1:

Run the following command:

find . -maxdepth 1 -type f ! -name "*.*" -exec grep -lvIP '[^[:ascii:]]' {} +
  • find is a more powerful ls.
  • . -maxdepth 1 means only the current directory
  • -type f means only files
  • ! -name "*.*" means excluding files with an extension
  • -exec grep...{} + means filter the list of files through the grep command
  • -lvIP '[^[:ascii:]]' means show only files -l which do not contain -v any non(^)-ascii characters, and also do not contain binary data(-I). Perl-syntax -P is required to use the [:ascii:] character class in the pattern.

Solution 2:

In Linux, any file may have any content. So, a file named a.txt may well contain a JPEG picture and a file named firmware.bin may well contain ASCII text.

If you are interested in the contents of the files (to contain "ASCII text") as well as the names of the files (not to contain a . in the file name), @vanadium's proposal in the comment of the other anwer may be improved like this:

find . -maxdepth 1 -type f ! -name "*.*" -exec file -0 {} \; | \
  grep -Pa "\0.*ASCII text.*$"

* This command correctly does not select "binary" files having ASCII text in their names.

To get only the list of file names that match, add the pipe | grep -Pao '^[^\0]*' to the end of the command given above.