Search all text files for a text string
Is there a quick way to search all text files for a string? By "text file" I mean ASCII files, like log files, config files etc, but excluding binaries like executables, database files, etc? What I want is something like: find / -type a -exec grep 192.168.100.1 \{\} \; .. or similar, except that I made up "-type a" for ascii. (I'm trying to find all places that an IP address is mentioned so that I can change ones that need to be changed.) -- Mark Rogers // More Solutions Ltd (Peterborough Office) // 0844 251 1450 Registered in England (0456 0902) 21 Drakes Mews, Milton Keynes, MK8 0ER
On Thu, 2016-06-30 at 11:14 +0100, Mark Rogers wrote:
Is there a quick way to search all text files for a string?
By "text file" I mean ASCII files, like log files, config files etc, but excluding binaries like executables, database files, etc?
What I want is something like: find / -type a -exec grep 192.168.100.1 \{\} \; .. or similar, except that I made up "-type a" for ascii.
(I'm trying to find all places that an IP address is mentioned so that I can change ones that need to be changed.)
Given that the "file" command can recurse through directories, I would have thought there was mileage in something like; grep '192\.168\.1\.1' `file -r | grep ascii | awk -F: {print $1}` Yes, yes, I know this is hideous and an 'xargs' would be good in there somewhere, but it's just a suggestion to start from! -- Today is Sweetmorn, the 35th day of Confusion in the YOLD 3182 I don't have an attitude problem. If you have a problem with my attitude, that's your problem.
On 30 June 2016 at 11:23, Huge <huge@huge.org.uk> wrote:
Given that the "file" command can recurse through directories,
file -r didn't seem to work for me. (-r = raw, not recurse). But it did lead me to the horrible messy but functional: grep 192.168.1.1 `file $(find etc -type f) | grep ASCII | sed 's#:.*##'` So thanks! Improvements welcome though (as a learning exercise). -- Mark Rogers // More Solutions Ltd (Peterborough Office) // 0844 251 1450 Registered in England (0456 0902) 21 Drakes Mews, Milton Keynes, MK8 0ER
On Thu, 2016-06-30 at 11:56 +0100, Mark Rogers wrote:
On 30 June 2016 at 11:23, Huge <huge@huge.org.uk> wrote:
Given that the "file" command can recurse through directories,
file -r didn't seem to work for me. (-r = raw, not recurse).
W-e-e-e-e-e-e-lll. I meant '-R', but that doesn't do what I thought it did anyway!
But it did lead me to the horrible messy but functional:
grep 192.168.1.1 `file $(find etc -type f) | grep ASCII | sed 's#:.*##'`
So thanks!
You're welcome.
Improvements welcome though (as a learning exercise).
-ditto- -- Today is Sweetmorn, the 35th day of Confusion in the YOLD 3182 I don't have an attitude problem. If you have a problem with my attitude, that's your problem.
On Thu, Jun 30, 2016 at 11:14:00AM +0100, Mark Rogers wrote:
Is there a quick way to search all text files for a string?
By "text file" I mean ASCII files, like log files, config files etc, but excluding binaries like executables, database files, etc?
What I want is something like: find / -type a -exec grep 192.168.100.1 \{\} \; .. or similar, except that I made up "-type a" for ascii.
(I'm trying to find all places that an IP address is mentioned so that I can change ones that need to be changed.)
Does "grep -r -I 192.168.100.1 /" not do what you want? J. -- Can I drink your juice? This .sig brought to you by the letter X and the number 32 Product of the Republic of HuggieTag
On 30 June 2016 at 12:16, Jonathan McDowell <noodles@earth.li> wrote:
Does "grep -r -I 192.168.100.1 /" not do what you want?
Ah, yes it probably does, now that I see that as an upper-case "i" rather than a lower-case "L"! Well I thought there should be a better way, and now I just feel a bit daft for not spotting it... Mark -- Mark Rogers // More Solutions Ltd (Peterborough Office) // 0844 251 1450 Registered in England (0456 0902) 21 Drakes Mews, Milton Keynes, MK8 0ER
On 30/06/16 12:16, Jonathan McDowell wrote: [SNIP]
Does "grep -r -I 192.168.100.1 /" not do what you want?
That won't work if there are lots and lots of files to search... Cheers, Laurie. -- --------------------------------------------------------------------- Laurie Brown laurie@brownowl.com ---------------------------------------------------------------------
On Thu, Jun 30, 2016 at 01:23:43PM +0100, Laurie Brown wrote:
On 30/06/16 12:16, Jonathan McDowell wrote:
Does "grep -r -I 192.168.100.1 /" not do what you want?
That won't work if there are lots and lots of files to search...
Why not? J. -- "The best thing about Debian is that it's boring." -- Peter Corlett
On 30/06/16 13:37, Jonathan McDowell wrote:
On Thu, Jun 30, 2016 at 01:23:43PM +0100, Laurie Brown wrote:
On 30/06/16 12:16, Jonathan McDowell wrote:
Does "grep -r -I 192.168.100.1 /" not do what you want?
That won't work if there are lots and lots of files to search...
Why not?
Because you it can fail with an "Argument list too long" message. It's a right pain, but using xargs as I previously suggested avoids that. Cheers, Laurie. -- --------------------------------------------------------------------- Laurie Brown laurie@brownowl.com ---------------------------------------------------------------------
On Thu, Jun 30, 2016 at 02:21:23PM +0100, Laurie Brown wrote:
On 30/06/16 13:37, Jonathan McDowell wrote:
On Thu, Jun 30, 2016 at 01:23:43PM +0100, Laurie Brown wrote:
On 30/06/16 12:16, Jonathan McDowell wrote:
Does "grep -r -I 192.168.100.1 /" not do what you want?
That won't work if there are lots and lots of files to search...
Why not?
Because you it can fail with an "Argument list too long" message. It's a right pain, but using xargs as I previously suggested avoids that.
"-r" means recursively grep; you pass a single directory to grep in the same way you pass a single directory to find in your example. The number of files under that directory is irrelevant. J. -- Web [ Time is an illusion. Lunchtime doubly so. ] site: http:// [ ] Made by www.earth.li/~noodles/ [ ] HuggieTag 0.0.24
On 30/06/16 14:55, Jonathan McDowell wrote:
On Thu, Jun 30, 2016 at 02:21:23PM +0100, Laurie Brown wrote:
On 30/06/16 13:37, Jonathan McDowell wrote:
On Thu, Jun 30, 2016 at 01:23:43PM +0100, Laurie Brown wrote:
On 30/06/16 12:16, Jonathan McDowell wrote:
Does "grep -r -I 192.168.100.1 /" not do what you want?
That won't work if there are lots and lots of files to search...
Why not?
Because you it can fail with an "Argument list too long" message. It's a right pain, but using xargs as I previously suggested avoids that.
"-r" means recursively grep; you pass a single directory to grep in the same way you pass a single directory to find in your example. The number of files under that directory is irrelevant.
Well, all I can say is that I was bitten by this a long time ago on a Gentoo box, using "-r", and the only way around it was to use xargs. Your mileage may vary, as the Yanks say, but if you google grep and "Argument list too long" you will find many, many people asking how to fix it, and xargs is the consistent answer. I've never had a problem since and I've been using Linux for well over 20 years. You use what works for you, that's the beauty of Linux. Cheers, Laurie. -- --------------------------------------------------------------------- Laurie Brown laurie@brownowl.com ---------------------------------------------------------------------
On Thu, Jun 30, 2016 at 02:21:23PM +0100, Laurie Brown wrote:
On 30/06/16 13:37, Jonathan McDowell wrote:
On Thu, Jun 30, 2016 at 01:23:43PM +0100, Laurie Brown wrote:
On 30/06/16 12:16, Jonathan McDowell wrote:
Does "grep -r -I 192.168.100.1 /" not do what you want?
That won't work if there are lots and lots of files to search...
Why not?
Because you it can fail with an "Argument list too long" message. It's a right pain, but using xargs as I previously suggested avoids that.
It very rarely happens to me nowadays though, in fact I don't think I've seen that error in years. -- Chris Green
On Thu, Jun 30, 2016 at 12:16:07PM +0100, Jonathan McDowell wrote:
On Thu, Jun 30, 2016 at 11:14:00AM +0100, Mark Rogers wrote:
Is there a quick way to search all text files for a string?
By "text file" I mean ASCII files, like log files, config files etc, but excluding binaries like executables, database files, etc?
What I want is something like: find / -type a -exec grep 192.168.100.1 \{\} \; .. or similar, except that I made up "-type a" for ascii.
(I'm trying to find all places that an IP address is mentioned so that I can change ones that need to be changed.)
Does "grep -r -I 192.168.100.1 /" not do what you want?
That was exactly what I was going to ask. :-) -- Chris Green
On 30/06/16 11:14, Mark Rogers wrote:
Is there a quick way to search all text files for a string?
By "text file" I mean ASCII files, like log files, config files etc, but excluding binaries like executables, database files, etc?
What I want is something like: find / -type a -exec grep 192.168.100.1 \{\} \; .. or similar, except that I made up "-type a" for ascii.
(I'm trying to find all places that an IP address is mentioned so that I can change ones that need to be changed.)
This is what I use: #### wild card grep: find [dir] -type f | xargs grep -i "string" #### find filenames containing string: find [dir] -type f | xargs grep -liH "string" Cheers, Laurie. -- --------------------------------------------------------------------- Laurie Brown laurie@brownowl.com ---------------------------------------------------------------------
On 30/06/2016 13:22, Laurie Brown wrote:
find [dir] -type f | xargs grep -i "string"
Also: find [dir] -type f -print0 | xargs -0 grep -i "string" This delimits names with ASCII null, so can handle filenames with embedded spaces, newlines and other weirdness. -- Red to red, black to black, switch it on, but stand well back.
On 04/07, Brett Parker wrote:
On 30 Jun 13:22, Laurie Brown wrote:
find [dir] -type f | xargs grep -liH "string"
Or, getting rid of the useless pipe, and using any modern version of find:
find [dir] -type f -exec grep -liH "string" {} +
I likely missed some context by not bothering to read the whole thread, but why not: grep -rliH "string" [dir] ? Steve
On Mon, 4 Jul 2016 15:36:04 +0100 Steve Engledow <steve@offend.me.uk> wrote:
On 04/07, Brett Parker wrote:
On 30 Jun 13:22, Laurie Brown wrote:
find [dir] -type f | xargs grep -liH "string"
Or, getting rid of the useless pipe, and using any modern version of find:
find [dir] -type f -exec grep -liH "string" {} +
I likely missed some context by not bothering to read the whole thread, but why not:
grep -rliH "string" [dir]
?
find: invalid predicate `-rliH' -- __ __| |_ __ __ .----------------------------------------------. / _/ _` \ V V / | mailto:alug_cdw@the-walker-household.co.uk | \__\__,_|\_/\_/ |______________________________________________|
On 4 July 2016 at 15:36, Steve Engledow <steve@offend.me.uk> wrote:
I likely missed some context by not bothering to read the whole thread, but why not:
grep -rliH "string" [dir]
The only bit you missed was the same suggestion from Mr McDowell, and yes it's the right (and simple) answer that I was missing! All the other ways to work around it with find were interesting, but grep -rl was definitely the key. -- Mark Rogers // More Solutions Ltd (Peterborough Office) // 0844 251 1450 Registered in England (0456 0902) 21 Drakes Mews, Milton Keynes, MK8 0ER
On Thu, 7 Jul 2016 10:40:45 +0100 Mark Rogers <mark@more-solutions.co.uk> wrote:
On 4 July 2016 at 15:36, Steve Engledow <steve@offend.me.uk> wrote:
I likely missed some context by not bothering to read the whole thread, but why not:
grep -rliH "string" [dir]
The only bit you missed was the same suggestion from Mr McDowell, and yes it's the right (and simple) answer that I was missing!
All the other ways to work around it with find were interesting, but grep -rl was definitely the key.
I have an app on my Sailfish phone and using these text searches I've been looking for any files related to it so that I can modify the QML files. Normally the files are /home/nemo.local/share/harbour-<appname> but they're not. So using the various search suggestions I've been searching but without success. Can anybody suggest why they're not working and also a way to find the files? -- __ __| |_ __ __ .----------------------------------------------. / _/ _` \ V V / | mailto:alug_cdw@the-walker-household.co.uk | \__\__,_|\_/\_/ |______________________________________________|
On Thu, 2016-07-07 at 10:40 +0100, Mark Rogers wrote:
grep -rl was definitely the key.
Some of us are older than the '-r' switch to 'grep'. :o) -- Today is Setting Orange, the 44th day of Confusion in the YOLD 3182 I don't have an attitude problem. If you have a problem with my attitude, that's your problem.
On 09-Jul-2016 15:36:25 Huge wrote:
On Thu, 2016-07-07 at 10:40 +0100, Mark Rogers wrote:
grep -rl was definitely the key.
Some of us are older than the '-r' switch to 'grep'.
:o)
From which it would follow that some of us haven't read 'man grep' for years :o):o) Ted.
-- Today is Setting Orange, the 44th day of Confusion in the YOLD 3182 I don't have an attitude problem. If you have a problem with my attitude, that's your problem.
------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding@wlandres.net> Date: 09-Jul-2016 Time: 23:08:27 This message was sent by XFMail -------------------------------------------------
participants (10)
-
Bill Hill -
Brett Parker -
Chris Green -
Chris Walker -
Huge -
Jonathan McDowell -
Laurie Brown -
Mark Rogers -
Steve Engledow -
Ted.Harding@wlandres.net