Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Just try to use:

bzgrep --help
grep through bzip2 files

Usage: bzgrep [grep_options] pattern [files]

For example, I need grep information from list of files by number 1941974:

'billing_log_1.bz'
'billing_log_2.bz'
'billing_log_3.bz'
'billing_log_4.bz'
'billing_log_5.bz'

What can I do?

bzgrep '1941974' billing_log_1

Continuous your code with fixes by bzcat:

find . -type f -name "*.bz2" |while read file
do 
    bzcat $file | grep -ia 'text string' | less
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top