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

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

    Why is it unhandy? If you need to use it often, and don't want to type it every time just define a bash function for it:

    function findTextInAsciiFiles {
        # usage: findTextInAsciiFiles DIRECTORY NEEDLE_TEXT
        find "$1" -type f -exec grep -l "$2" {} \; -exec file {} \; | grep text
    }
    

    put it in your .bashrc and then just run:

    findTextInAsciiFiles your_folder "needle text"
    

    whenever you want.


    EDIT to reflect OP's edit:

    if you want to cut out mime informations you could just add a further stage to the pipeline that filters out mime informations. This should do the trick, by taking only what comes before :: cut -d':' -f1:

    function findTextInAsciiFiles {
        # usage: findTextInAsciiFiles DIRECTORY NEEDLE_TEXT
        find "$1" -type f -exec grep -l "$2" {} \; -exec file {} \; | grep text | cut -d ':' -f1
    }
    

提交回复
热议问题