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

前端 未结 16 908
孤街浪徒
孤街浪徒 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 04:52

    find . -type f -print0 | xargs -0 file | grep -P text | cut -d: -f1 | xargs grep -Pil "search"
    

    This is unfortunately not space save. Putting this into bash script makes it a bit easier.

    This is space safe:

    #!/bin/bash
    #if [ ! "$1" ] ; then
        echo "Usage: $0 ";
        exit
    fi
    
    find . -type f -print0 \
      | xargs -0 file \
      | grep -P text \
      | cut -d: -f1 \
      | xargs -i% grep -Pil "$1" "%"
    

提交回复
热议问题