Skip to content

Scan files with the CLI

Docs for v0.11.1 · Go 1.27.0+ · Go API

The go-yara command compiles rules and scans files. Put the rules file first, followed by options and the target path. Binaries and build instructions are on the installation page.

In a shell, create a rule and a sample file, then scan it:

Create and scan a sample
cat > greeting.yar <<'YARA'
rule greeting {
strings:
$text = "hello" nocase
condition:
$text
}
YARA
printf 'Hello from Go\n' > sample.txt
go-yara greeting.yar sample.txt
go-yara greeting.yar --json -s sample.txt

The first scan reports the matching rule, greeting. The second emits a JSON record with the rule name and string-match details, including an occurrence at offset 0.

To compile without scanning a target:

Terminal window
go-yara greeting.yar --mode=compile

Use --mode=lex to inspect tokens or --mode=parse to inspect the parsed rule structure. For scan output, -s includes matching strings, -g includes tags, and -m includes metadata. --json produces structured records suitable for downstream tools. Place options before a positional target.

For a directory, use -r to recurse and -p to choose the worker count. Use -i to filter by rule name or -t to filter by tag.

This example creates two sample files, including one in a nested directory. -r includes that directory, -p 2 runs two scan workers, and -g -m includes tags and metadata:

Recursive scan with metadata
set -eu
cat > catalog.yar <<'YARA'
rule document : inventory {
meta:
category = "example"
strings:
$text = "hello" nocase
condition:
$text
}
YARA
mkdir -p cli-samples/nested
printf 'hello\n' > cli-samples/one.txt
printf 'HELLO\n' > cli-samples/nested/two.txt
go-yara catalog.yar -r -p 2 --json -g -m cli-samples

In this normal scan mode, --json writes one JSON object per matching rule and target, each on its own line. The example produces two records named document, tagged inventory, with meta.category equal to example. Parse the output line by line rather than as a JSON array. Matched byte data can be included in JSON output; handle it according to the sensitivity of the scanned files.

A successful normal scan returns status 0 whether or not a rule matches. With --json, a file with no matching rules produces no records. Operational errors return status 1 and a diagnostic on stderr. Check the exit status before interpreting empty output as no matches; this CLI does not use grep-style exit status to distinguish matches from non-matches.

The directory scanner gathers results before printing them. If a target fails to read or scan, the command returns an error; do not treat the output as a complete inventory of the directory. Use --json -c when you need a count record for each successfully scanned target, including zero.

--streaming is a literal text-pattern reporting mode. It does not evaluate complete rule conditions. It is not equivalent to a normal scan with lower memory use, and it does not provide regex or hex-pattern rule evaluation.

Use it only when literal occurrences are sufficient. --chunk-size controls chunked I/O; it does not add full-condition semantics. For normal rule evaluation, plan for the input memory required by the scan API.

This output comes from the CLI built at the release shown at the top of the page. The --streaming description in the help is brief; the limitation above applies whenever that flag is used.

go-yara --help
Usage: go-yara <yara-file> [options] [target]
Options:
-c print only number of matches (shorthand)
-chunk-size int
chunk size in bytes (default: 1MB) (default 1048576)
-count
print only number of matches
-data string
data file to match against (for --mode=execute)
-early-termination
enable early termination when matches found
-fast-scan
fast scan mode, skips match offset extraction
-g print tags (shorthand)
-i string
print only rules with name <ident> (shorthand)
-identifier string
print only rules with name <ident>
-json
emit structured JSON output
-m print metadata (shorthand)
-match-context int
include n bytes before and after each match in execute output
-match-data int
include up to n matched bytes in execute output
-max-match-span int
bound maximum bytes evaluated for a single regex match (default: unlimited)
-mode string
processing mode: lex, parse, compile, execute (default: compile or execute if target given)
-n print only not satisfied rules (shorthand)
-negate
print only not satisfied rules
-no-matches
alias for --fast-scan
-p int
number of worker threads for parallel scanning (shorthand)
-print-meta
print metadata
-print-strings
print matching strings
-print-tags
print tags
-r recursively search directories (shorthand)
-recursive
recursively search directories
-s print matching strings (shorthand)
-streaming
enable streaming processing for large files
-t string
print only rules tagged as <tag> (shorthand)
-tag string
print only rules tagged as <tag>
-threads int
number of worker threads for parallel scanning (default: NumCPU)
Modes: lex, parse, compile (default), execute