Search a directory of *.txt file for a string of text

Is there a software to do flexible text string search of all the *.txt files in a folder ?

For example, i have a lot of different categories of txt files in a folder, inside each of the file, there are a lot of document of different topics that might intertwine.
To get the right document that i need, sometime i need to open out multiple txt files and do multiple search strings to find what i am looking for.
Hence i ask if there such a powerful text string search software, that able to do a search on a folder that is full of *.txt to find the string that i am looking for in the *.txt ?

Have you tried grep --help

3 Likes

Have you tried google?

1 Like

One possibility is Catfish - it can do content searches, and list all the files it finds with those contents…

as suggested by @kjw, you can start with basic grep such as grep -irH for case insensitive, recursive (if spanning subdirs) and printing the filename for each match so you can better filter out files.
You can also try ripgrep (fancy new implementation of the classic, and ripgrep-all, if you’d like to extend your search to pdfs and docs for example).
More difficult (for me, intended) are awk and sed, but those are more flexible apparently.
If you wanna opt for GUIs, usually DEs have their own implementation (eg. catfish on xfce and tracker in gnome).

2 Likes

Here is an example grep -r “example text” /home/tom/

1 Like

grep -r -i "pattern" /path
(-r means recursive, -i means case insensitive)

To replace a pattern, use sed:
sed -i 's/pattern/replacement/g' /path/to/file
(-i means in-place editing, the final g means replace every instance, not just the first)

Both are very powerful tools. Sometimes sed/grep expressions are hard to read when using extended regular expressions as patterns.
Simple search/replace/delete tasks are easy to learn though.

1 Like

Old school way
find path -name \*.txt -exec grep -Hn pattern {} \;
man find and man grep are your friends.

1 Like
1 Like

Ha! I never expect grep is so powerful. Me thought it only has it place in something like:
$cat abc.txt | grep “find_me”
Never know it can search thru directory.
May i know what format does grep support ? does it able to search text in pdf files as well ?
If grep seaches into a folder with *.txt , *.pdf, *.html… is there way to tell it to exclude certain file extension or include certain file extension so that it won’t come out with a lot of junk result ?

Sorry, i post all my question now altogether, because last night, the forum server is under maintenance i guess, it does not allow me to post anything.

No. Only text files. Well not quite, I think you should be able to grep for binary code as well, but who does that? :slight_smile:
EDIT: yes you can grep binaries, it’s documented in the manpage

Yeah, unfortunately this usage of pipes is still used too often.
It’s simply grep "find_me" abc.txt.

grep supports regular expressions and wildcards (globs).
You can use --exclude-dir or --exclude=pattern options and of course the equivalent include.
Also, -v is useful, it inverts the grepping.

lol… it seems like i need to continue to bury my head in text again to read the whole doc to try to use the grep command. :sweat_smile:

Always helpful and shouldn’t be underestimated.

6 Likes

:nerd_face:Yes, it is the time that we have is limited. I have not being doing any real work that i want on linux. Because the learning curve is simply too high, at least for me. I would like to read everything and know everything without needed to wait for ppl in forum, but i couldn’t… no such knowledge to everything mentioned in manuals… and no such time… :sweat_smile:
If i was on linux every since my first computer, then it would be no problem to me… but sadly i was on microsoft windows every since my first computer… have no idea what linux is back then… also i think linux back then was nothing more than “dos”. lol
What i asked in forum, all those info that i got, i am still reading them right now! lol i was refering back to this thread and many other thread. I appreciate those ppl who show me good info… i surely go thru all, even more… so thanks

Well that was the case for me too, the first computers I used at work were Windoze.
For most stuff I recommend reading man and info pages.
google/duckduck go, are your friends too, usually I get hits on stackoverflow or stackexchange.
But those are not the unfiltered truth so a pinch of skepticism should be applied.
And of course this post in my opinion and a pinch of skepticism should be applied as well :slight_smile: