Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
1  
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
add comment

5 Answers

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. –  faif Mar 20 '11 at 1:59
add comment

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
add comment

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
    
Thanks for your thanks, but you are supposed to mark your accepted answer with an check mark. This will get you away from the 0% accept rate and improve the chances to get answers in the future. (Disclosure: It would give me some extra reputation too). –  user unknown Apr 27 '11 at 3:57
1  
@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
show 1 more comment

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

Just try file * .* | fgrep ASCII

share|improve this answer
add comment

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

ls $1 | while read name
    do
        # "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...
        fi
    done
share|improve this answer
add comment

Your Answer

 
discard

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.