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

前端 未结 16 895
孤街浪徒
孤街浪徒 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:09

    How about this:

    $ grep -rl "needle text" my_folder | tr '\n' '\0' | xargs -r -0 file | grep -e ':[^:]*text[^:]*$' | grep -v -e 'executable'
    

    If you want the filenames without the file types, just add a final sed filter.

    $ grep -rl "needle text" my_folder | tr '\n' '\0' | xargs -r -0 file | grep -e ':[^:]*text[^:]*$' | grep -v -e 'executable' | sed 's|:[^:]*$||'
    

    You can filter-out unneeded file types by adding more -e 'type' options to the last grep command.

    EDIT:

    If your xargs version supports the -d option, the commands above become simpler:

    $ grep -rl "needle text" my_folder | xargs -d '\n' -r file | grep -e ':[^:]*text[^:]*$' | grep -v -e 'executable' | sed 's|:[^:]*$||'
    

提交回复
热议问题