Linux search text string from .bz2 files recursively in subdirectories

Semu picture Semu · Jan 7, 2014 · Viewed 25.6k times · Source

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.

Answer

hek2mgl picture hek2mgl · Jan 7, 2014

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.