Saturday, May 11, 2024
14
rated 0 times [  14] [ 0]  / answers: 1 / hits: 3726  / 1 Year ago, sat, december 10, 2022, 4:07:37

I am looking for a terminal way. Open all files in /foo/bar containing the string foobar. How?


More From » command-line

 Answers
1

You didn't say how you want to open them :)



One way is to use grep to recursively look for the string in your files (-r). Use -l to ask grep to output only the filenames of matching files. This will output one filename per line, use xargs to build a suitable command line for your editor.



grep -r -l foobar /foo/bar/ | xargs vim


this also works with gedit; just change vim for gedit.



You can also use find for this but it's more complicated IMHO:



find /foo/bar -type -f -exec grep -l foobar {} ;  | xargs vim

[#32797] Sunday, December 11, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
amacal

Total Points: 457
Total Questions: 102
Total Answers: 116

Location: Thailand
Member since Thu, Apr 22, 2021
3 Years ago
amacal questions
Thu, Aug 11, 22, 17:35, 2 Years ago
Thu, Apr 13, 23, 18:00, 1 Year ago
Sat, Oct 22, 22, 09:15, 2 Years ago
;