Saltar al contenido

~ / archivo / stackoverflow / 51098172

Shell command to list file names where the matching is found

StackOverflow · shell, grep, pipe · ayudé
  1. sqr pregunta +1 2018-06-29 09:08

    I am trying to search through a list of binary files to find some keywords on Mac.

    The following works to list out all the matches, but it doesn't show me the list of files where it is being found:

    find . -type f -exec strings {} \;|grep "Bv9qtsZRgspQliITY4"
    

    Is there any trick to do this?

    • Biffen 2018-06-29 10:07 Do you still want to print the matches, or just the files’ names/paths?
  2. Biffen respuesta aceptada +1 2018-06-29 09:24

    Using -exec with a wee ‘script’:

    find . -type f \
           -exec sh -c 'strings "$1" | grep -q "Bv9qtsZRgspQliITY4"' -- {} \; \
           -print
    

    The above will print the paths of all the matching files. If you also want to print the matches you can use:

    find . -type f \
           -exec sh -c 'strings "$1" | grep "Bv9qtsZRgspQliITY4"' -- {} \; \
           -print
    

    This will, however, print the paths after the matches. If this is not desirable, then you can use:

    find . -type f \
           -print \
           -exec sh -c 'strings "$1" | grep "Bv9qtsZRgspQliITY4"' -- {} \;
    

    This, on the other hand, will print all paths, even non-matching ones. To print only matching paths and their matches:

    find . -type f \
           -exec sh -c 'strings "$1" | grep -q "Bv9qtsZRgspQliITY4"' -- {} \; \
           -print \
           -exec grep "Bv9qtsZRgspQliITY4" {} \;
    

    This will run grep twice on matching files, which will make it slower. If this is a problem the matches can be stored in a variable, and if there are any the path printed first and then the matches. This is left as an exercise to the reader.*

    * Let me know if I should post it here.

    • Miguel Ortiz yo 2018-06-29 10:04 I've tested this one, but didn't display the content of the file.
    • Miguel Ortiz yo 2018-06-29 10:12 I thought the user wanted both things, filenames and below the strings printed. In that case grep already does that.
    • Miguel Ortiz yo 2018-06-29 10:24 True, what about this: $ grep -arl . -e 'soloman' | tee | strings --print-file-name In this case you could redirect to strings and process there?
  3. user464014 respuesta -1 2018-06-29 09:12

    Try grep -rl "Bv9qtsZRgspQliITY4" ..

    Explanation of options:

    • -r: search recursively
    • -l: don't print the contents of the file, just print the filename.

    Optionally, you might want to use -i to search case-insensitively.

    The problem with your idea is that you're piping the output of strings into grep. The filename is only passed to strings, meaning that nothing that comes after strings knows the filename.

  4. iLoveTux respuesta -1 2018-06-29 09:14

    I'm not quite sure about portability, but if you are using GNU's version of grep, then you can use --files-with-matches

    -l, --files-with-matches  print only names of FILEs containing matches
    

    Then you can use something like this:

    grep --recursive --files-with-matches "Bv9qtsZRgspQliITY4" *
    
    • Biffen 2018-06-29 09:43 You’re missing the whole strings thing. This probably won’t work.
    • Biffen 2018-06-29 09:45 FYI, -l is portable (POSIX), but --files-with-matches is not (GNU).
    • iLoveTux 2018-06-29 13:46 GNU grep will search binary files. If you want to be explicit about it you can pass -U to specify a binary search. so the strings command and the associated find command are superfluous.
    • Biffen 2018-06-29 13:49 But strings’s output is not the same as the contents of the file, so greping it is not the same as greping the file, binary or not.
    • iLoveTux 2018-06-29 13:54 I agree, but the OP only wants to find filenames so the differences are not going to affect the output. Also earlier I said -U I meant to say -a.
    • Biffen 2018-06-29 13:57 Yeah, almost. strings also doesn’t prints strings from the whole file, but may limit itself to strings ‘in loadable, initialized data sections’, so the grep results might differ. But since OP has gone missing we might never know whether that matters, or even if they want to print matches as well as filenames.
    • iLoveTux 2018-06-29 14:46 he will probably come back and wonder why people are still discussing because he solved his problem and moved onto the next as we all tend to do. There were three almost identical answers right away so he most likely used those and it worked for him.
  5. nandal respuesta -1 2018-06-29 09:14

    You can use

    # this will list all the files containing given text in current directory
    # i to ignore case
    # l to list files with matches
    # R read and process all files in that directory, recursively, following all symbolic links
    grep -iRl "your-text-to-find" ./
    
    # for case sensitive search
    grep -Rl "your-text-to-find" ./
    
    • Miguel Ortiz yo 2018-06-29 09:18 Adding -i could lead to incorrect strings in the search of the user.
    • Biffen 2018-06-29 09:43 @nandal You’re missing the whole strings thing. This probably won’t work.
    • sqr 2018-06-29 14:36 @John, thank you, fyi, this prints out the file names, without the matches.
  6. Miguel Ortiz yo respuesta -1 2018-06-29 09:17

    Well, if it's only to print names of files don't use find but grep.

    grep -ar . -e 'soloman' ./testo.txt:1:soloman
    
    • -a : Search in binary files
    • -r : recursive

    And keep it simple.

    If you don't want to see the words matched in your output simply add -l, --files-with-matches:

    user@DESKTOP-RR909JI ~/projects/search
    $ grep -arl . -e 'soloman'
    ./testo.txt
    

← todos los hilos de StackOverflow