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.
Scan a file
Section titled “Scan a file”In a shell, create a rule and a sample file, then scan it:
cat > greeting.yar <<'YARA'rule greeting { strings: $text = "hello" nocase condition: $text}YARAprintf 'Hello from Go\n' > sample.txtgo-yara greeting.yar sample.txtgo-yara greeting.yar --json -s sample.txtThe 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.
Validate rules and inspect output
Section titled “Validate rules and inspect output”To compile without scanning a target:
go-yara greeting.yar --mode=compileUse --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.
Scan a directory and consume JSON
Section titled “Scan a directory and consume JSON”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:
set -eucat > catalog.yar <<'YARA'rule document : inventory { meta: category = "example" strings: $text = "hello" nocase condition: $text}YARAmkdir -p cli-samples/nestedprintf 'hello\n' > cli-samples/one.txtprintf 'HELLO\n' > cli-samples/nested/two.txtgo-yara catalog.yar -r -p 2 --json -g -m cli-samplesIn 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.
Exit status and errors
Section titled “Exit status and errors”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.
Large files and streaming
Section titled “Large files and streaming”--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.
Command help
Section titled “Command help”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.
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