• Get a list of files that contain a string

    From Marco Moock@21:1/5 to All on Tue Sep 26 15:02:19 2023
    Hello!

    What is the best way to get a list of files that contain a specific
    string without printing the content?

    I can use find to select all files in a directory including subdirs and
    I can use grep to filter it, but it prints the content.

    Is there a better solution for that?

    --
    kind regards
    Marco

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christian Weisgerber@21:1/5 to Marco Moock on Tue Sep 26 13:16:58 2023
    On 2023-09-26, Marco Moock <mm+usenet-es@dorfdsl.de> wrote:

    What is the best way to get a list of files that contain a specific
    string without printing the content?

    grep -l

    --
    Christian "naddy" Weisgerber naddy@mips.inka.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Marco Moock on Tue Sep 26 16:54:19 2023
    On 26.09.2023 15:02, Marco Moock wrote:
    Hello!

    What is the best way to get a list of files that contain a specific
    string without printing the content?

    I can use find to select all files in a directory including subdirs and
    I can use grep to filter it, but it prints the content.

    Is there a better solution for that?

    If you don't need to "find" the files it's just

    grep -lF specific-string ...list-of-files-and-or-file-patterns...

    or if you have to find the files

    find ...file-properties... | xargs grep -lF specific-string

    It it's not a "specific string" but a regular expression pattern that
    you want to match in the files then omit grep's F option, use just -l.

    If filenames contain blanks and whatnot, add option -print0 to find
    and -0 (this is a 'dash zero') to xargs.

    Alternatively you may also use find's -exec option instead of xargs.

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Elvidge@21:1/5 to Marco Moock on Tue Sep 26 15:46:10 2023
    On 26/09/2023 14:02, Marco Moock wrote:
    Hello!

    What is the best way to get a list of files that contain a specific
    string without printing the content?

    I can use find to select all files in a directory including subdirs and
    I can use grep to filter it, but it prints the content.

    Is there a better solution for that?


    Forget find, use grep -rl (or -Rl) (recursive, list)


    --
    Chris Elvidge, England
    A TRAINED APE COULD NOT TEACH GYM

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Chris Elvidge on Tue Sep 26 16:57:45 2023
    On 26.09.2023 16:46, Chris Elvidge wrote:

    Forget find, use grep -rl (or -Rl) (recursive, list)

    Yes - ...in case your grep has it. (It's non-standard.)

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Marco Moock on Wed Sep 27 10:13:27 2023
    On 27.09.2023 10:06, Marco Moock wrote:
    Am 26.09.2023 um 16:57:45 Uhr schrieb Janis Papanagnou:

    Yes - ...in case your grep has it. (It's non-standard.)

    Mine has it.

    (Mine too.)

    [...]

    Most Linux distributions ship GNU grep, some UNIX systems might have
    other variants.

    Sure, but in your OP you haven't told us in what environment you
    are working or whether you have portability demands. So providing
    a standard solution or pointing out non-standard ones is mandatory.

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marco Moock@21:1/5 to All on Wed Sep 27 10:05:33 2023
    Am 26.09.2023 um 15:46:10 Uhr schrieb Chris Elvidge:

    Forget find, use grep -rl (or -Rl) (recursive, list)

    Thanks, that is fine.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marco Moock@21:1/5 to All on Wed Sep 27 10:06:53 2023
    Am 26.09.2023 um 16:57:45 Uhr schrieb Janis Papanagnou:

    Yes - ...in case your grep has it. (It's non-standard.)

    Mine has it.

    grep 3.11-3 amd64 GNU grep, egrep and fgrep

    OS is Debian.

    Most Linux distributions ship GNU grep, some UNIX systems might have
    other variants.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Spiros Bousbouras@21:1/5 to Janis Papanagnou on Thu Sep 28 04:42:49 2023
    On Tue, 26 Sep 2023 16:54:19 +0200
    Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
    or if you have to find the files

    find ...file-properties... | xargs grep -lF specific-string

    It it's not a "specific string" but a regular expression pattern that
    you want to match in the files then omit grep's F option, use just -l.

    If filenames contain blanks and whatnot, add option -print0 to find
    and -0 (this is a 'dash zero') to xargs.

    Alternatively you may also use find's -exec option instead of xargs.

    I note that grep also has the -q option to not print output.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Spiros Bousbouras on Thu Sep 28 11:44:46 2023
    On 28.09.2023 06:42, Spiros Bousbouras wrote:

    I note that grep also has the -q option to not print output.

    Yes, this is useful when you intend to use it as predicate

    if grep -q ... ; then : found ; else : not found ; fi

    and aren't interested in the output (the match or the file names).

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ed Morton@21:1/5 to Marco Moock on Sun Oct 1 06:56:58 2023
    On 9/26/2023 8:02 AM, Marco Moock wrote:
    Hello!

    What is the best way to get a list of files that contain a specific
    string without printing the content?

    I can use find to select all files in a directory including subdirs and
    I can use grep to filter it, but it prints the content.

    Is there a better solution for that?


    Use "find" to find files and "grep" to g/re/p (Globally match a Regular Expression and Print) within files:

    find . -type f -exec grep -Fl 'string' {} +

    Note the `-F` to make it a string rather then regexp comparison.

    The above will work on any Unix system as it's only using mandatory
    POSIX tools/options.

    Regards,

    Ed.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)