← 返回命令列表

Linux command

zgrep 命令

文件

复制后可按需替换文件名、目录或参数。

常用示例

Search for pattern in gzipped file

zgrep "[pattern]" [file.gz]

Case-insensitive search

zgrep -i "[pattern]" [file.gz]

Show line numbers

zgrep -n "[pattern]" [file.gz]

Search recursively in compressed files

zgrep -r "[pattern]" [directory]

Count matching lines

zgrep -c "[pattern]" [file.gz]

Show only filenames with matches

zgrep -l "[pattern]" [*.gz]

说明

zgrep searches for patterns in gzip-compressed files without manual decompression. It's equivalent to gunzip -c file.gz | grep pattern but more convenient. The tool automatically detects whether files are compressed and handles them appropriately. This makes it safe to use on directories containing mixed compressed and uncompressed files. zgrep supports the same options as grep, including basic and extended regular expressions. For explicit regex modes, use zegrep (extended) or zfgrep (fixed strings). Multiple files can be searched, with filenames shown by default when multiple files match.

参数

-i
Case-insensitive matching
-n
Show line numbers
-l
List filenames with matches only
-c
Count matching lines
-v
Invert match (show non-matching lines)
-h
Suppress filename in output
-r, -R
Recursive search
-E
Extended regular expressions (like egrep)
-F
Fixed string matching (like fgrep)
-e _pattern_
Specify pattern

FAQ

What is the zgrep command used for?

zgrep searches for patterns in gzip-compressed files without manual decompression. It's equivalent to gunzip -c file.gz | grep pattern but more convenient. The tool automatically detects whether files are compressed and handles them appropriately. This makes it safe to use on directories containing mixed compressed and uncompressed files. zgrep supports the same options as grep, including basic and extended regular expressions. For explicit regex modes, use zegrep (extended) or zfgrep (fixed strings). Multiple files can be searched, with filenames shown by default when multiple files match.

How do I run a basic zgrep example?

Run `zgrep "[pattern]" [file.gz]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -i do in zgrep?

Case-insensitive matching