All Articles

The ABC's of grep - J

J

Use the -J flag to decompress bzip2 files before searching.

Let’s return back to our simple test.txt file with the following content:

sad
happy
awake
coffee
work
school

To compress the test.txt file using bzip2 run:

$ bzip2 test.txt

This is a bit of silly example since test.txt is so small that compressing it will actually make it bigger, but you should now see a compressed test.txt.bz2 file.

If you look inside test.txt.bz2 you will see nothing but gobbly gook (yes, that’s the technical term). As you can probably imagine, our normal way of searching for the word “happy” will not work.

$ cat test.txt.bz2
BZh91AY&SY7PA?/Lؠ "&??z?4)??rG???R[?K=li@???H?
???%
$ grep "happy" test.txt.bz2 

This is where the -J flag comes in:

$ grep -J "happy' test.txt.bz2
happy

Published Nov 5, 2023

I love coffee, coding and writing.