I have a case where multiple .bz2 files are situated in subdirectories. And I want to search for a text, from all files, using bzcat and grep command linux commands.
I am able to search one-one file by using the following command:
bzcat <filename.bz2> | grep -ia 'text string' | less
But I now I need to do the above for all files in subdirectories.
Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines. Using the grep command, we can recursively search all files for a string on a Linux.
The bzgrep utility is used to invoke the grep utility on bzip2 compressed files. All options specified are passed directly to grep. If no file is specified, the standard input is decompressed if necessary and fed to grep. Otherwise, the given files are decompressed (if necessary) and fed to grep.
You need to use the grep command. The grep command or egrep command searches the given input FILEs for lines containing a match or a text string.
Grep is a Linux / Unix command-line tool used to search for a string of characters in a specified file. The text search pattern is called a regular expression. When it finds a match, it prints the line with the result. The grep command is handy when searching through large log files.
You can use bzgrep
instead of bzcat
and grep
. This is faster.
To grep recursively in a directory tree use find
:
find -type f -name '*.bz2' -execdir bzgrep "pattern" {} \;
find
is searching recursively for all files with the *.bz2
extension and applies the command specified with -execdir
to them.
There are several methods:
bzgrep regexp $(find -name \*.bz2)
This method will work if number of the found files is not very big (and they have no special characters in the pathes). Otherwise you better use this one:
find -name \*.bz2 -exec bzgrep regexp {} /dev/null \;
Please note /dev/null
in the second method. You use it to make bzgrep
print the filename,
where the regexp
was found.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With