Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to write a shell script which searches the current UNIX directory and returns the names of all files of type ASCII text?

share|improve this question
The script you've shown above doesn't seem to relate to the question at all. That's odd. – mattdm Mar 19 '11 at 3:02

The best of 2 worlds: Avoids the use of the useless xargs, and speeds things up, since the + triggers parallel invocation.

find . -type f -exec file {} + | grep ASCII
share|improve this answer
+1 for the +, I never knew that – jamespo Apr 2 '11 at 10:36
Yes, but I need to add, that this is a gnu-find extension. Most of the time I forget about that. – user unknown Apr 2 '11 at 18:37
great, congrats – Host Post Apr 26 '11 at 21:31
@user unknown: "+"' isn't a gnu-find extension. It was first introduced by SVR4 find then adopted by the POSIX standard and later included by Gnu find. Every current find implementation is very likely to implement it. – jlliagre May 9 '11 at 20:20
Thanks @jiliagre, my horizon is to tall, I just know a little bit about about Linux, and enjoy to learn new facts. – user unknown May 9 '11 at 20:29

Exec 'file' on all the files in the current directoy, and then grep for 'ASCII':

find . -maxdepth 1 -exec file {} \; | grep ASCII
share|improve this answer
How portable is grep ASCII? I remember that once I had problems because the output of file is not exactly the same on all systems but I don't recall if it was related with ASCII files. – ierax Mar 20 '11 at 1:59

find . -type f -print0 | xargs -0 file | grep ASCII

On CentOS 5, ASCII can mean a lot of things such as "ASCII C++ program text", "ASCII English text", and "ASCII text" so you might need to narrow it down more.

share|improve this answer

If it is just the current directory, no need for find.

Just try file * .* | fgrep ASCII

share|improve this answer

Assume you get directory name as the argument ($1), then,

ls $1 | while read name
        # "file" returns file type
        file $1/$name | grep -i 'ascii' &> /dev/null

        # $? gives exit status of previous command
        if [ $? -eq 0 ]; then
            # $1/$name is your ascii file, process it here...
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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