Score:0

How to list all ASCII files present in a directory?

mx flag

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?

Score:6
cn flag

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.
Score:4
cn flag

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.

Score:0
in flag

This can be accomplished with find:

find . -maxdepth 1 -type f ! -name "*.*"

Note that ! means NOT, so will remove any result containing a ..

vanadium avatar
cn flag
This will list *all* files without extension, not only ASCII text files as the OP asks. The intent of OP appears to be to retrieve all ASCII files, even it they do not have an extension.
vanadium avatar
cn flag
Could become `find . -maxdepth 1 -type f ! -name "*.*" -exec file {} \; | grep "ASCII text"` to find ASCI files only without extension
FedKad avatar
cn flag
@vanadium This is not a perfect solution, since it will match non-ASCII files with `ASCII text` in part of their names. However, the OP is not clear when talking about "ASCII files".
skekmal avatar
mx flag
@FedonKadifeli Can you elaborate on my not knowing about ASCII files ? Are there different types of ASCII files that we can use here to narrow down the search?
FedKad avatar
cn flag
@skekmal In Linux, any file 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 as well as the names of the files, @vanadium's proposal may be improved like this: `find . -maxdepth 1 -type f ! -name "*.*" -exec file -0 {} \; | grep -Pa "\0.*ASCII text"`
vanadium avatar
cn flag
@FedonKadifeli that is why I did not write out as a full answer
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.