How to read all human readable files in Bash?

I take an intro course on UNIX and ask the following question:

How many files in the previous question are text files? A text file is any file containing human-readable content. (TRICK QUESTION) Run the file command in the file to see if the file is a text file or a binary data file! If you simply count the number of files with the extension .txt, you will not have points for this question.)

The previous question simply asked how many regular files there were, which was easy to figure out by running find . -type f | wc -l.

I'm just having trouble deciding what “human readable content” is, since I assume it means anything other than binary / precast, but I thought it was being displayed -type f. Maybe this is what the professor had in mind when he said "trick"?

This question has the following description, which also asks: "What text files contain the string" csc "in any combination of upper and lower case?". Obviously, “text” is not just about files .txt, but I need to find out the first question to determine this!

+5
source share
2 answers

Quotes added for clarity:

"" , , !

file , . "" () .

:

desktop.ini:   Little-endian UTF-16 Unicode text, with CRLF, CR line terminators
tw2-wasteland.jpg: JPEG image data, JFIF standard 1.02

, file .

, " ", , - /, , - f.

find -type f . , , . , : , , .

, , , ""?

, , find -name '*.txt' - ​​ . . UNIX, Windows. !


, , .

? # 1, , , , . , , .

  • - xargs, . xargs , stdin .

    $ find . -type f | xargs file
    ./netbeans-6.7.1.desktop: ASCII text
    ./VMWare.desktop:         a /usr/bin/env xdg-open script text executable
    ./VMWare:                 cannot open `./VMWare' (No such file or directory)
    (copy).desktop:           cannot open `(copy).desktop' (No such file or directory)
    ./Eclipse.desktop:        a /usr/bin/env xdg-open script text executable
    
  • . . . script.

    , VMWare (copy).desktop, . xargs . , xargs -0 NUL . NUL, .

    $ find . -type f -print0 | xargs -0 file
    ./netbeans-6.7.1.desktop: ASCII text
    ./VMWare.desktop:         a /usr/bin/env xdg-open script text executable
    ./VMWare (copy).desktop:  a /usr/bin/env xdg-open script text executable
    ./Eclipse.desktop:        a /usr/bin/env xdg-open script text executable
    
  • script, , . , , .

    $ find . -type f -exec file {} \;
    ./netbeans-6.7.1.desktop: ASCII text
    ./VMWare.desktop:         a /usr/bin/env xdg-open script text executable
    ./VMWare (copy).desktop:  a /usr/bin/env xdg-open script text executable
    ./Eclipse.desktop:        a /usr/bin/env xdg-open script text executable
    

    , -exec file , {} , . \; file.

+6

, , file --mime-type <filename> 'text/plain'. , .txt

, :

FILES=`find $YOUR_DIR -type f`

for file in $FILES ;
do

mime=`/usr/bin/file --mime-type $YOUR_DIR/$file | /bin/sed 's/^.* //'`

if [ $mime = "text/plain" ];  then      
    fileTotal=$(( fileTotal + 1 ))
    echo "$fileTotal - $file"
fi

done

echo "$fileTotal human readable files found!"

:

1 - /sampledir/samplefile
2 - /sampledir/anothersamplefile
....
23 human readable files found!

mime-, (, HTML / XML-?), http://www.feedforall.com/mime-types.htm

0

All Articles