Grep to return entire file content
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
up vote
1
down vote
favorite
I want to search recursively for a certain word, say MyWord, but I want to print the entire contents of all files that contain MyWord. How do I do this?
It turns out that I particularly care about binary sqlite files, both grepping them, and reading the result as a text file, rather than binary.
command-line grep
add a comment |Â
up vote
1
down vote
favorite
I want to search recursively for a certain word, say MyWord, but I want to print the entire contents of all files that contain MyWord. How do I do this?
It turns out that I particularly care about binary sqlite files, both grepping them, and reading the result as a text file, rather than binary.
command-line grep
1
Are you sure you want to read the entire content of each matching file? judicious application of grep's context switches (-A
,-B
,-C
) may be more useful
â steeldriver
Apr 18 at 1:58
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I want to search recursively for a certain word, say MyWord, but I want to print the entire contents of all files that contain MyWord. How do I do this?
It turns out that I particularly care about binary sqlite files, both grepping them, and reading the result as a text file, rather than binary.
command-line grep
I want to search recursively for a certain word, say MyWord, but I want to print the entire contents of all files that contain MyWord. How do I do this?
It turns out that I particularly care about binary sqlite files, both grepping them, and reading the result as a text file, rather than binary.
command-line grep
command-line grep
edited Apr 18 at 1:05
muru
130k19273463
130k19273463
asked Apr 17 at 22:26
Baron Yugovich
11616
11616
1
Are you sure you want to read the entire content of each matching file? judicious application of grep's context switches (-A
,-B
,-C
) may be more useful
â steeldriver
Apr 18 at 1:58
add a comment |Â
1
Are you sure you want to read the entire content of each matching file? judicious application of grep's context switches (-A
,-B
,-C
) may be more useful
â steeldriver
Apr 18 at 1:58
1
1
Are you sure you want to read the entire content of each matching file? judicious application of grep's context switches (
-A
, -B
, -C
) may be more usefulâ steeldriver
Apr 18 at 1:58
Are you sure you want to read the entire content of each matching file? judicious application of grep's context switches (
-A
, -B
, -C
) may be more usefulâ steeldriver
Apr 18 at 1:58
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
I would use:
sudo grep -rIlZ --exclude=$HISTFILE##*/ --exclude-dir=boot,dev,lib,media,mnt,proc,root,run,sys,/tmp,tmpfs,var '/' -e 'MyWord' | xargs -0 cat > MyOutputFile
- Drop
sudo
if not looking in system directories. - Change (root directory)
'/'
to current directory'.'
or specific directory ie'/home/me/stuff'
- Remove
mnt
to include Windows files mounted undermnt
. - Remove
lib
to include Linux source files - Excluding directories necessary for speed. See: `grep`ing all files for a string takes a long time
--exclude=$HISTFILE##*/
is necessary if you run the command a second time or if the search stringMyWord
has ever been used in a different command before. This prevents 5 thousand history lines ($HISTFILE) from being included.-r
recursive,I
skip binary files,lZ
outputs a zero byte after each file name instead of the usual newline. Null (zero byte) terminators necessary forperl -0
,sort -z
andxargs
used tocat
(print out) the file contents.> MyOutputFile
sends output to file instead of screen. Leave this off for output to screen.
Note: Missing from output is the file name, just the file content is listed.
How do I write it to an output file?
â Baron Yugovich
Apr 17 at 23:11
@BaronYugovich I've testedcat > MyOutputFile
and excluded 5 thousand lines of bash terminal history file from output.
â WinEunuuchs2Unix
Apr 18 at 0:01
Excellent answer (+1)!
â JJoao
May 11 at 10:47
add a comment |Â
up vote
1
down vote
You can also use find
with grep
:
find . -type f -exec grep -wq 'MyWord' ; -exec cat >> /path/to/outfile ;
This will find files containing the word "MyWord" recursively and quite as soon as first match found then redirect the whole file content to a new file.
How do I write it to an output file?
â Baron Yugovich
Apr 17 at 23:01
Thanks. I do have one more follow-up: the resulting files are sometimes binary, what's the best way to view them as text? This is Mozilla Firefox cached files from browser history.
â Baron Yugovich
Apr 17 at 23:24
to reading a binary file as a text file, you can add-a
option to thegrep
above if you have GNU grep.
â Ã±ÃÂsýù÷
Apr 18 at 2:40
add a comment |Â
up vote
0
down vote
If your folder are not so big try:
grep -rz '.*MyWord.*' myfolder > output
if necessary add -a
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
I would use:
sudo grep -rIlZ --exclude=$HISTFILE##*/ --exclude-dir=boot,dev,lib,media,mnt,proc,root,run,sys,/tmp,tmpfs,var '/' -e 'MyWord' | xargs -0 cat > MyOutputFile
- Drop
sudo
if not looking in system directories. - Change (root directory)
'/'
to current directory'.'
or specific directory ie'/home/me/stuff'
- Remove
mnt
to include Windows files mounted undermnt
. - Remove
lib
to include Linux source files - Excluding directories necessary for speed. See: `grep`ing all files for a string takes a long time
--exclude=$HISTFILE##*/
is necessary if you run the command a second time or if the search stringMyWord
has ever been used in a different command before. This prevents 5 thousand history lines ($HISTFILE) from being included.-r
recursive,I
skip binary files,lZ
outputs a zero byte after each file name instead of the usual newline. Null (zero byte) terminators necessary forperl -0
,sort -z
andxargs
used tocat
(print out) the file contents.> MyOutputFile
sends output to file instead of screen. Leave this off for output to screen.
Note: Missing from output is the file name, just the file content is listed.
How do I write it to an output file?
â Baron Yugovich
Apr 17 at 23:11
@BaronYugovich I've testedcat > MyOutputFile
and excluded 5 thousand lines of bash terminal history file from output.
â WinEunuuchs2Unix
Apr 18 at 0:01
Excellent answer (+1)!
â JJoao
May 11 at 10:47
add a comment |Â
up vote
2
down vote
I would use:
sudo grep -rIlZ --exclude=$HISTFILE##*/ --exclude-dir=boot,dev,lib,media,mnt,proc,root,run,sys,/tmp,tmpfs,var '/' -e 'MyWord' | xargs -0 cat > MyOutputFile
- Drop
sudo
if not looking in system directories. - Change (root directory)
'/'
to current directory'.'
or specific directory ie'/home/me/stuff'
- Remove
mnt
to include Windows files mounted undermnt
. - Remove
lib
to include Linux source files - Excluding directories necessary for speed. See: `grep`ing all files for a string takes a long time
--exclude=$HISTFILE##*/
is necessary if you run the command a second time or if the search stringMyWord
has ever been used in a different command before. This prevents 5 thousand history lines ($HISTFILE) from being included.-r
recursive,I
skip binary files,lZ
outputs a zero byte after each file name instead of the usual newline. Null (zero byte) terminators necessary forperl -0
,sort -z
andxargs
used tocat
(print out) the file contents.> MyOutputFile
sends output to file instead of screen. Leave this off for output to screen.
Note: Missing from output is the file name, just the file content is listed.
How do I write it to an output file?
â Baron Yugovich
Apr 17 at 23:11
@BaronYugovich I've testedcat > MyOutputFile
and excluded 5 thousand lines of bash terminal history file from output.
â WinEunuuchs2Unix
Apr 18 at 0:01
Excellent answer (+1)!
â JJoao
May 11 at 10:47
add a comment |Â
up vote
2
down vote
up vote
2
down vote
I would use:
sudo grep -rIlZ --exclude=$HISTFILE##*/ --exclude-dir=boot,dev,lib,media,mnt,proc,root,run,sys,/tmp,tmpfs,var '/' -e 'MyWord' | xargs -0 cat > MyOutputFile
- Drop
sudo
if not looking in system directories. - Change (root directory)
'/'
to current directory'.'
or specific directory ie'/home/me/stuff'
- Remove
mnt
to include Windows files mounted undermnt
. - Remove
lib
to include Linux source files - Excluding directories necessary for speed. See: `grep`ing all files for a string takes a long time
--exclude=$HISTFILE##*/
is necessary if you run the command a second time or if the search stringMyWord
has ever been used in a different command before. This prevents 5 thousand history lines ($HISTFILE) from being included.-r
recursive,I
skip binary files,lZ
outputs a zero byte after each file name instead of the usual newline. Null (zero byte) terminators necessary forperl -0
,sort -z
andxargs
used tocat
(print out) the file contents.> MyOutputFile
sends output to file instead of screen. Leave this off for output to screen.
Note: Missing from output is the file name, just the file content is listed.
I would use:
sudo grep -rIlZ --exclude=$HISTFILE##*/ --exclude-dir=boot,dev,lib,media,mnt,proc,root,run,sys,/tmp,tmpfs,var '/' -e 'MyWord' | xargs -0 cat > MyOutputFile
- Drop
sudo
if not looking in system directories. - Change (root directory)
'/'
to current directory'.'
or specific directory ie'/home/me/stuff'
- Remove
mnt
to include Windows files mounted undermnt
. - Remove
lib
to include Linux source files - Excluding directories necessary for speed. See: `grep`ing all files for a string takes a long time
--exclude=$HISTFILE##*/
is necessary if you run the command a second time or if the search stringMyWord
has ever been used in a different command before. This prevents 5 thousand history lines ($HISTFILE) from being included.-r
recursive,I
skip binary files,lZ
outputs a zero byte after each file name instead of the usual newline. Null (zero byte) terminators necessary forperl -0
,sort -z
andxargs
used tocat
(print out) the file contents.> MyOutputFile
sends output to file instead of screen. Leave this off for output to screen.
Note: Missing from output is the file name, just the file content is listed.
edited Apr 18 at 0:00
answered Apr 17 at 23:09
![](https://i.stack.imgur.com/2SXNl.jpg?s=32&g=1)
![](https://i.stack.imgur.com/2SXNl.jpg?s=32&g=1)
WinEunuuchs2Unix
35.7k759133
35.7k759133
How do I write it to an output file?
â Baron Yugovich
Apr 17 at 23:11
@BaronYugovich I've testedcat > MyOutputFile
and excluded 5 thousand lines of bash terminal history file from output.
â WinEunuuchs2Unix
Apr 18 at 0:01
Excellent answer (+1)!
â JJoao
May 11 at 10:47
add a comment |Â
How do I write it to an output file?
â Baron Yugovich
Apr 17 at 23:11
@BaronYugovich I've testedcat > MyOutputFile
and excluded 5 thousand lines of bash terminal history file from output.
â WinEunuuchs2Unix
Apr 18 at 0:01
Excellent answer (+1)!
â JJoao
May 11 at 10:47
How do I write it to an output file?
â Baron Yugovich
Apr 17 at 23:11
How do I write it to an output file?
â Baron Yugovich
Apr 17 at 23:11
@BaronYugovich I've tested
cat > MyOutputFile
and excluded 5 thousand lines of bash terminal history file from output.â WinEunuuchs2Unix
Apr 18 at 0:01
@BaronYugovich I've tested
cat > MyOutputFile
and excluded 5 thousand lines of bash terminal history file from output.â WinEunuuchs2Unix
Apr 18 at 0:01
Excellent answer (+1)!
â JJoao
May 11 at 10:47
Excellent answer (+1)!
â JJoao
May 11 at 10:47
add a comment |Â
up vote
1
down vote
You can also use find
with grep
:
find . -type f -exec grep -wq 'MyWord' ; -exec cat >> /path/to/outfile ;
This will find files containing the word "MyWord" recursively and quite as soon as first match found then redirect the whole file content to a new file.
How do I write it to an output file?
â Baron Yugovich
Apr 17 at 23:01
Thanks. I do have one more follow-up: the resulting files are sometimes binary, what's the best way to view them as text? This is Mozilla Firefox cached files from browser history.
â Baron Yugovich
Apr 17 at 23:24
to reading a binary file as a text file, you can add-a
option to thegrep
above if you have GNU grep.
â Ã±ÃÂsýù÷
Apr 18 at 2:40
add a comment |Â
up vote
1
down vote
You can also use find
with grep
:
find . -type f -exec grep -wq 'MyWord' ; -exec cat >> /path/to/outfile ;
This will find files containing the word "MyWord" recursively and quite as soon as first match found then redirect the whole file content to a new file.
How do I write it to an output file?
â Baron Yugovich
Apr 17 at 23:01
Thanks. I do have one more follow-up: the resulting files are sometimes binary, what's the best way to view them as text? This is Mozilla Firefox cached files from browser history.
â Baron Yugovich
Apr 17 at 23:24
to reading a binary file as a text file, you can add-a
option to thegrep
above if you have GNU grep.
â Ã±ÃÂsýù÷
Apr 18 at 2:40
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You can also use find
with grep
:
find . -type f -exec grep -wq 'MyWord' ; -exec cat >> /path/to/outfile ;
This will find files containing the word "MyWord" recursively and quite as soon as first match found then redirect the whole file content to a new file.
You can also use find
with grep
:
find . -type f -exec grep -wq 'MyWord' ; -exec cat >> /path/to/outfile ;
This will find files containing the word "MyWord" recursively and quite as soon as first match found then redirect the whole file content to a new file.
edited Apr 17 at 23:13
answered Apr 17 at 22:44
ñÃÂsýù÷
23.3k2191152
23.3k2191152
How do I write it to an output file?
â Baron Yugovich
Apr 17 at 23:01
Thanks. I do have one more follow-up: the resulting files are sometimes binary, what's the best way to view them as text? This is Mozilla Firefox cached files from browser history.
â Baron Yugovich
Apr 17 at 23:24
to reading a binary file as a text file, you can add-a
option to thegrep
above if you have GNU grep.
â Ã±ÃÂsýù÷
Apr 18 at 2:40
add a comment |Â
How do I write it to an output file?
â Baron Yugovich
Apr 17 at 23:01
Thanks. I do have one more follow-up: the resulting files are sometimes binary, what's the best way to view them as text? This is Mozilla Firefox cached files from browser history.
â Baron Yugovich
Apr 17 at 23:24
to reading a binary file as a text file, you can add-a
option to thegrep
above if you have GNU grep.
â Ã±ÃÂsýù÷
Apr 18 at 2:40
How do I write it to an output file?
â Baron Yugovich
Apr 17 at 23:01
How do I write it to an output file?
â Baron Yugovich
Apr 17 at 23:01
Thanks. I do have one more follow-up: the resulting files are sometimes binary, what's the best way to view them as text? This is Mozilla Firefox cached files from browser history.
â Baron Yugovich
Apr 17 at 23:24
Thanks. I do have one more follow-up: the resulting files are sometimes binary, what's the best way to view them as text? This is Mozilla Firefox cached files from browser history.
â Baron Yugovich
Apr 17 at 23:24
to reading a binary file as a text file, you can add
-a
option to the grep
above if you have GNU grep.â Ã±ÃÂsýù÷
Apr 18 at 2:40
to reading a binary file as a text file, you can add
-a
option to the grep
above if you have GNU grep.â Ã±ÃÂsýù÷
Apr 18 at 2:40
add a comment |Â
up vote
0
down vote
If your folder are not so big try:
grep -rz '.*MyWord.*' myfolder > output
if necessary add -a
add a comment |Â
up vote
0
down vote
If your folder are not so big try:
grep -rz '.*MyWord.*' myfolder > output
if necessary add -a
add a comment |Â
up vote
0
down vote
up vote
0
down vote
If your folder are not so big try:
grep -rz '.*MyWord.*' myfolder > output
if necessary add -a
If your folder are not so big try:
grep -rz '.*MyWord.*' myfolder > output
if necessary add -a
answered May 11 at 10:44
JJoao
1,24059
1,24059
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1025951%2fgrep-to-return-entire-file-content%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Are you sure you want to read the entire content of each matching file? judicious application of grep's context switches (
-A
,-B
,-C
) may be more usefulâ steeldriver
Apr 18 at 1:58