By Manny Fernandez

September 24, 2026

Mastering grep on the FortiGate CLI: Every Switch, Explained

If you live in the FortiGate CLI, grep is the difference between scrolling through 4,000 lines of show output and landing on the exact line you need in one shot. FortiOS ships a trimmed-down grep that you pipe onto get, show, and diagnose output. It is not GNU grep, and it has one Fortinet-specific trick that makes it far more useful than the generic version.

This post covers every switch, what it does, and when to reach for it.

Background

Grep has been on FortiOS for a long time. Community reports place the basic flag set as far back as FortiOS 4.0 MR3. The context-aware -f switch arrived later in the 5.x era and is the one most engineers still do not know about.

The quickest way to see what your build supports is to feed grep an invalid flag and read the usage banner:

FGT # show | grep -X
grep: invalid option -- X
Usage: grep [-invfcABC] PATTERN

The Switch Reference

Switch Name What it does
(none) Basic match Prints every line containing the pattern
-i Ignore case Case-insensitive match
-v Invert Prints lines that do NOT match
-n Line numbers Prefixes each match with its line number
-c Count Prints only the number of matching lines
-f Fortinet context Prints the full config block around each match
-A <num> After Prints NUM lines of trailing context
-B <num> Before Prints NUM lines of leading context
-C <num> Context Prints NUM lines before and after

Basic Syntax

<get | show | diagnose ...> | grep [switches] <pattern>

Wrap any pattern that contains spaces in double quotes, and escape inner quotes with a backslash:

show firewall policy | grep "set srcintf \"port1\""

-i: Ignore Case

Object names in FortiOS are case-sensitive, but humans are not consistent. Use -i when you are not sure how someone named an object.

show firewall address | grep -i webserver

This catches WebServer, WEBSERVER, and webserver-dmz in one pass.

-v: Invert Match

Strip out the noise. Great for filtering out lines you already know about.

get system interface physical | grep -v "status: down"
diagnose sys session list | grep -v "proto=17"

The second example removes UDP sessions from session list output so TCP stands out.

-n: Line Numbers

Useful when you are correlating grep output with a full show dump you exported, or when you want a sense of where in the config something lives.

show | grep -n "set vdom"

-c: Count Only

Returns a single number instead of the matching lines. This is the quick-audit switch.

show firewall policy | grep -c "edit"
show firewall policy | grep -c "set action deny"
show firewall policy | grep -c "set logtraffic disable"

In three commands you know your total policy count, how many are explicit denies, and how many policies are not logging. That last number belongs in every QBR.

-f: Fortinet Context (The Killer Feature)

This is the switch that makes FortiOS grep worth learning. Standard grep has no idea that a set line belongs to an edit block, which belongs to a config tree. The -f switch does: it returns every match wrapped in its complete configuration hierarchy.

Without -f:

FGT # show | grep ldap-group1
    edit "ldap-group1"
            set groups "ldap-group1"

You know the group exists and is referenced somewhere. That is it.

With -f:

FGT # show | grep -f ldap-group1
config user group
    edit "ldap-group1"
        set member "pc40-LDAP"
    next
end
config firewall policy
    edit 2
        set srcintf "port31"
        set dstintf "port32"
        ...
        set groups "ldap-group1"
    next
end

Now you see the object definition AND every policy that references it, each wrapped in its full config / edit / next / end structure. Practical uses:

show | grep -f "WEB-SRV-01"           # where is this address used?
show firewall policy | grep -f wan1   # every policy touching wan1
show | grep -f "set status disable"   # every disabled object, in context

A big bonus: -f output is valid CLI syntax. You can copy a block, edit it, and paste it into another FortiGate.

Gotcha: -f only makes sense against show output, since that is where the config hierarchy exists. On get or diagnose output it has no structure to work with, so use -A, -B, or -C there instead.

-A, -B, -C: Line Context

These give you raw line counts around a match. They are the right tool for diagnose and get output, which have no config hierarchy.

-A (after) prints lines following the match:

diagnose sys session list | grep -A 12 "dport=443"

-B (before) prints lines preceding the match:

get router info routing-table all | grep -B 2 "10.0.0.0/16"

-C (context) prints lines on both sides:

diagnose vpn ike gateway list | grep -C 5 "HQ-TUNNEL"
get system performance status | grep -C 3 "Memory"

Before -f existed, the standard workaround for viewing a whole policy was a large context window such as grep -C 20. That still works, but -f is cleaner because it stops exactly at block boundaries instead of guessing.

Combining Switches

Switches stack. A few combos worth memorizing:

show | grep -fi "guest"                     # context + case-insensitive
show firewall policy | grep -ci "accept"    # case-insensitive count
diagnose sys session list | grep -in -A 5 "198.18.10.5"

Quick Reference Cheat Sheet

Goal Command
Find everywhere an object is used show | grep -f "<object>"
Count firewall policies show firewall policy | grep -c edit
Count policies not logging show firewall policy | grep -c "logtraffic disable"
Case-insensitive object hunt show firewall address | grep -i "<name>"
Hide down interfaces get system interface physical | grep -v "status: down"
Session detail for a host diagnose sys session list | grep -A 12 "<ip>"
Route lookup with neighbors get router info routing-table all | grep -C 2 "<prefix>"
VPN tunnel detail diagnose vpn ike gateway list | grep -C 5 "<tunnel>"

Troubleshooting and Gotchas

No output at all. Check quoting first. If your pattern contains spaces or quotation marks, wrap it in double quotes and escape inner quotes with a backslash.

-f returns too much. A short pattern like port1 also matches port10 through port19. Include the surrounding quotes from the config line to anchor on the exact object name:

show | grep -f "\"port1\""

Missing default values. show only displays non-default settings, so grep cannot find what is not printed. Use show full-configuration when you need to search defaults:

show full-configuration system global | grep -i timeout

Not GNU grep. Do not expect -E, -o, -w, -r, or long options like --color. If the usage banner does not list it, your build does not support it.

VDOM scope. On multi-VDOM units, grep only sees the output of the command you piped it from. Enter the correct VDOM (or config global) before searching.

Recent posts

  • If you've spent any time configuring user authentication on... Full Story

  • DNS is one of those technologies that quietly underpins... Full Story

  • BGP issues on FortiGate firewalls usually trace back to... Full Story

  • Every time your laptop talks to your router, a... Full Story

  • If you've spent any time configuring NAT on a... Full Story

  • If you have spent any time configuring firewall policies... Full Story

  • High availability on FortiGate is one of those features... Full Story

  • If you've configured SD-WAN on a FortiGate, you've almost... Full Story

  • FortiLink is the management protocol that turns a FortiSwitch... Full Story

  • FortiSwitches are pretty rock solid from Mean Time Between... Full Story

  • This is a quicky tip.  Have you ever gone... Full Story

  • DNS is one of those quiet pieces of internet... Full Story

  • This article is an updated version of the previous... Full Story

  • You will add ns2 as a secondary (slave) BIND9... Full Story

  • In the process of deploying my lab, I needed... Full Story

  • RFC 8805, used to be known as Self-Correcting IP... Full Story

  • Years back, I wrote an article about certificate pinning. ... Full Story

  • FortiGates have the ability to send alerts to Microsoft... Full Story

  • In this post, I am going to walk through... Full Story

  • Troubleshooting VoIP on a FortiGate can feel like trying... Full Story

  • Prior to FortiOS 7.0, there were three commands to... Full Story

  • In this post, I am going to go over... Full Story

  • What we are going to do:  We are going... Full Story

  • Choosing between FGCP (FortiGate Clustering Protocol) and FGSP (FortiGate... Full Story

  • Creating a VLAN on macOS (The "Pro" Move) A... Full Story

  • This blog post explores the logic behind how macOS... Full Story

  • Pretty Fly for a Wi-Fi Tell My Wi-Fi Love... Full Story

  • Part of my daily gig is creating BoMs (Bill-of-Materials)... Full Story

  • ICMP introduces several security risks, but careful filtering, rate... Full Story

  • The command diag debug application dhcps -1 enables full... Full Story

  • In the world of FortiOS, execute tac report is... Full Story

  • LLDP; What is it The Link Layer Discovery Protocol... Full Story

  • What it actually does When you run diagnose fdsm... Full Story

  • Monkey Bites are bite-sized, high-impact security insights designed for... Full Story

  • I have run macOS in macOS with Parallels but... Full Story

  • Don't be confused with my other FortiNAC posts where... Full Story

  • This is the third session in a multi-part article... Full Story

  • Today I was configuring key-based authentication on a FortiGate... Full Story

  • Netcat, often called the "Swiss Army knife" of networking,... Full Story

  • At its core, IEEE 802.1X is a network layer... Full Story

  • In case you did not see the previous FortiNAC... Full Story

  • This is our 5th session where we are going... Full Story

  • Now that we have Wireshark installed and somewhat configured,... Full Story

  • The Philosophy of Packet Analysis Troubleshooting isn't about looking... Full Story

  • Executive Summary Objective: Get HopMatrix installed, verified, and working... Full Story

  • Executive Summary Objective: Stand up RIPv2 between two FortiGates,... Full Story

  • Executive Summary Every FortiGate IPsec write-up tells you to... Full Story