Search code examples
linuxrecursiongrepbzip2

Linux search text string from .bz2 files recursively in subdirectories


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.


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.