All Articles

The ABC's of grep - Z

Z

The -Z​ flag will output the zero byte when used in combination with a flag that outputs the file name on a search match.​ Ok, what does that mean?

Let’s take a look at our test.txt file with the following content:

sad
happy
awake
coffee
work
school

If we wanted to list all the file names with the pattern “happy” in it, we would do the following:

$ grep -l "happy" *
test.txt
$

Note the newline after test.txt and the location of the cursor.

Using the -Z flag will add a zero byte instead of a newline, putting the cursor directly after file name.

$ grep -lZ "happy" *
test.txt:happy$

Published Nov 26, 2023

I love coffee, coding and writing.