Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Get a list of files that contain a string

21 views
Skip to first unread message

Marco Moock

unread,
Sep 26, 2023, 9:02:29 AM9/26/23
to
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

Christian Weisgerber

unread,
Sep 26, 2023, 9:30:11 AM9/26/23
to
On 2023-09-26, Marco Moock <mm+use...@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 na...@mips.inka.de

Chris Elvidge

unread,
Sep 26, 2023, 10:46:18 AM9/26/23
to
Forget find, use grep -rl (or -Rl) (recursive, list)


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

Janis Papanagnou

unread,
Sep 26, 2023, 10:54:26 AM9/26/23
to
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

Janis Papanagnou

unread,
Sep 26, 2023, 10:57:53 AM9/26/23
to
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

Marco Moock

unread,
Sep 27, 2023, 4:05:41 AM9/27/23
to
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.

Marco Moock

unread,
Sep 27, 2023, 4:07:00 AM9/27/23
to
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.

Janis Papanagnou

unread,
Sep 27, 2023, 4:13:34 AM9/27/23
to
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

Spiros Bousbouras

unread,
Sep 28, 2023, 12:42:56 AM9/28/23
to
On Tue, 26 Sep 2023 16:54:19 +0200
Janis Papanagnou <janis_pap...@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.

Janis Papanagnou

unread,
Sep 28, 2023, 5:44:55 AM9/28/23
to
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

Ed Morton

unread,
Oct 1, 2023, 7:57:05 AM10/1/23
to
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.
0 new messages