Linux command: How to 'find' only text files?

前端 未结 16 885
孤街浪徒
孤街浪徒 2020-12-02 04:43

After a few searches from Google, what I come up with is:

find my_folder -type f -exec grep -l \"needle text\" {} \\; -exec file {} \\; | grep text


        
相关标签:
16条回答
  • 2020-12-02 05:15

    Here's how I've done it ...

    1 . make a small script to test if a file is plain text istext:

    #!/bin/bash
    [[ "$(file -bi $1)" == *"file"* ]]
    

    2 . use find as before

    find . -type f -exec istext {} \; -exec grep -nHi mystring {} \;
    
    0 讨论(0)
  • 2020-12-02 05:16

    Although it is an old question, I think this info bellow will add to the quality of the answers here.

    When ignoring files with the executable bit set, I just use this command:

    find . ! -perm -111
    

    To keep it from recursively enter into other directories:

    find . -maxdepth 1 ! -perm -111
    

    No need for pipes to mix lots of commands, just the powerful plain find command.

    • Disclaimer: it is not exactly what OP asked, because it doesn't check if the file is binary or not. It will, for example, filter out bash script files, that are text themselves but have the executable bit set.

    That said, I hope this is useful to anyone.

    0 讨论(0)
  • 2020-12-02 05:17

    How about this

     find . -type f|xargs grep "needle text"
    
    0 讨论(0)
  • 2020-12-02 05:18
    • bash example to serach text "eth0" in /etc in all text/ascii files

    grep eth0 $(find /etc/ -type f -exec file {} \; | egrep -i "text|ascii" | cut -d ':' -f1)

    0 讨论(0)
提交回复
热议问题