Thursday, March 9, 2017

Find text inside compressed files


You have a group of files and some of them are compressed with zip or gz and you want to search for a specific text in each of them. To do this, you can use the following command:
find -name [FILE_PATTERN] -print0 | xargs -0 zgrep "[TEXT_TO_FIND]"
Where:
[FILE_PATTERN] : is the pattern of the files.
[TEXT_TO_FIND] : is the text to find inside the files.

Let's say we have the following files:

peter-01.gz
peter-02
peter-03.gz
peterA
peterB.gz
maria-01
maria-02.gz

And you want to find the text "my friend" only in the files that begininng with "peter-". To do this, you can use the following:
find -name peter-\* -print0 | xargs -0 zgrep "my friend"

Note the use of  \* as a wildcard.




Related Posts:

0 comments:

Post a Comment

Popular Posts

Recent Posts

Unordered List

Text Widget

Pages

Search This Blog

Powered by Blogger.