0

This question already has an answer here:

Is there a way to search for a string test123 in all textfiles on the hdd?

marked as duplicate by Jakuje, Jeff Schaller, garethTheRed, don_crissti, Anthon Mar 9 '16 at 20:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

You can use find to get all the .txt files and then grep the desired string

$ find / -type f -name '*.txt' -exec grep 'test123' {} +

Where:

  • / search in all the server.
  • -type f find only files, not directories
  • -name '*.txt' find all .txt files
  • -exec grep 'test123' search test123 in all the files found
  • {} is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone
  • + it will improve execution time significantly (since it will contatenate arguments prior to execution until)
  • Why the downvote? – tachomi Mar 9 '16 at 18:57
  • Sorrry was a mistake. i changed it. – Craxxurz Mar 9 '16 at 19:02
  • I used your command but the output was only "test123" – Craxxurz Mar 9 '16 at 19:03
  • @Craxxurz I updated – tachomi Mar 9 '16 at 19:08
1

Assuming that by "all textfiles" you mean al files ending in .txt:

find / -type f -name '*.txt' -exec grep -i 'test123' {} \;

or

find / -type f -name '*.txt' -exec grep -i 'test123' {} +
  • 1
    Can you explain the differences between your solutions? – fd0 Mar 9 '16 at 18:49
  • + is better because it doesn't run grep for every file found. – MichalH Mar 9 '16 at 18:54
1
find / -type f -exec grep -I test123 {} +

Version with + instead of \; is much faster and-I ignores binary files. Or simply use recursive grep:

grep -Ir test123 /

You probably also want to 2>/dev/null because there are going to be a lot of "permission denied" lines.

-1

Something to point you in the right direction:

find / -type f -name *.txt to find all .txt files, assuming you only have one hdd. And then just use grep to search in these files.

  • Can you give me all commands? I am new to Linux. – Craxxurz Mar 9 '16 at 18:44
  • Tachomis answer should work. – geruetzel Mar 9 '16 at 18:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.