Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 354
Semu Avatar asked Jan 07 '14 11:01

Semu


People also ask

How do you search for a string in all files recursively in Linux?

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.

How do I grep in bz2 files?

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.

How do I find a specific string or word in files and directories?

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.

How do you find a string in all files in a directory in Unix?

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.


2 Answers

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.

like image 120
hek2mgl Avatar answered Sep 28 '22 09:09

hek2mgl


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.

like image 34
Igor Chubin Avatar answered Sep 28 '22 08:09

Igor Chubin