These 9 uncommon grep flags are the secret to finding anything from the Linux terminal


If your typical grep workflow involves piping output through three other commands just to get what you need, then you’re doing it wrong. Grep has dozens of powerful flags that can help you count the number of lines, limit the output, extract patterns, and a whole lot more. Here are nine grep flags to help you find anything from the Linux terminal.

-w: Search for a standalone string,

Filter through partial matches

When you search for the term “log” using grep, it returns every single line containing “log”—even as a substring. Lines with “logfile,” “syslog,” and “catalog” all show up in the output. By using the -w flag, you can narrow the results to just the word “log.” It uses spaces, punctuation, and line boundaries to isolate your search term as a standalone word, showing only relevant results. For example:

grep -w "log" system.log

Here, grep is searching system.log for lines where “log” appears as a standalone word, not part of a longer string.

-c: Get a count of all the matching lines

Useful when you need a number, not a wall of output

Sometimes you just want to know how many lines contain a match—not necessarily read through each line individually. This is where grep’s -c flag comes in handy. It replaces grep’s default output and only prints the count of matching lines. This saves you from needing to pipe anything into wc -l. For example:

grep -c "404" access.log

Here, grep counts how many lines in access.log contain “404” and returns just that number instead of printing each matching line.

-m: Stop searching after a set number of matches

For when you only need the first few matches

By default, grep will scan the entire file and print every single match. This can be overwhelming if the file is really large, and potentially redundant if all you needed was the first few entries. The -m flag solves this issue by setting a hard limit on how many results grep fetches before it stops. For example:

grep -m 5 "timeout" server.log

Here, grep stops as soon as it finds five lines containing “timeout” in server.log, skipping everything after the fifth match entirely.

-C: Show context lines above and below each match

Do limit yourself to just the matching line

Oftentimes, finding a matching line is only half the picture. You need to read the context—the lines around the match—to understand what actually happened. For example, an error message in a log file, on its own, doesn’t reveal what triggered it or what came after. This is where you can use the -C flag.

grep -C 3 "error" app.log

It prints a set number of lines above and below each match. You get a small window around every result, enough to understand the situation without opening the file separately.

–include: Restrict recursive searches to specific file patterns

To help you narrow down your searches

A recursive grep on a large folder digs through every file type—scripts, configs, logs, markdown, all of it. When you already know which files you’re looking for, most of that scanning is wasted effort. The –include flag filters by filename pattern before grep even opens anything, limiting results to files that match the glob you specify. For example:

grep -r --include="*.conf" "timeout" ./

Here, -r tells grep to search recursively from the current directory, and –include=”*.conf” limits it to configuration files only. You can also target specific filenames like –include=”Makefile” or use multiple patterns like –include=”*.{js,ts}” to cast a wider net. There’s also an –exclude flag that you can pair with this one for more targeted queries.

-o: Print only the matching text, not the full line

Grep’s default output, where it prints the entire line containing your match, can be overwhelming, especially if all you want is a specific pattern or string. Here, you can use the -o flag to strip away everything except the matched text itself—printing only what the pattern captured. Combined with -E for extended regex, you can write a precise pattern and pull out exactly what you need. For example:

grep -oE "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+" access.log

Here, -o limits the output to just the matched text, and -E enables extended regex for the IP pattern—so you get a clean list of IPs instead of full log lines.

-s: Silence error messages from files grep can’t read

Stop grep from yelling at you

When you run a recursive grep across system directories or folders with restricted files, grep prints a “Permission denied” error for every file it can’t read something. In a directory like /etc/ with dozens of locked-down entries, those errors can quickly drown out the actual results. The -s flag suppresses these error messages entirely, letting only genuine matches come through. It doesn’t change which files grep searches or skip any accessible content—it just keeps your terminal clean. For example:

grep -rs "root" /etc/

Here, -r searches /etc/ recursively and -s silently skips every file grep can’t access, instead of printing an error for each one.

-v: Return every line that doesn’t match your pattern

For when you need to do a ‘reverse’ grep

Most people reach for grep when they want to find something, but it’s equally useful for filtering things out. The -v flag inverts the match—it returns every line that does not contain your search term. This makes it particularly handy for stripping noise from config files, like removing comment lines so you can see only the active settings. It works with any pattern grep supports, including regular expressions. For example:

grep -v "^#" nginx.conf

Here, ^# targets lines starting with a # comment, and -v inverts the logic—so the output contains everything except those commented lines.

-l: Show only the filenames that contain a match

When the file containing the text is more important

Searching across multiple files with grep usually returns more detail than you need—every matching line from every file, all at once. The -l flag simplifies this by suppressing all the line content and printing only the names of files that contain at least one match. It pairs naturally with -r for tracking down the specific files in an entire directory tree. For example:

grep -rl "API_KEY" ./src/

Here, -r searches through ./src/ recursively, and -l returns only the file paths containing “API_KEY”—no line numbers, no content, just the paths.

A Linux Terminal open on PopOS.


These 5 Linux Commands Make Reading Large Files Easier

Quality, not quantity.


Grep is a lot more powerful than most of us give it credit for

There you have it—nine grep flags to save you from reaching for a second command or having to open a file manually. Once a few of these become muscle memory, you’ll spend less time processing grep’s output and more time acting on it. Start with the ones that match your most common frustrations and build from there.

Kubuntu Focus M2 Gen 6 laptop.

8/10

Operating System

Kubuntu 24.04 LTS

CPU

Intel Core Ultra 9 275HX (2.7GHz up to 5.4GHz)

GPU

NVIDIA GeForce RTX 5070 Ti (dGPU), Intel Graphics (iGPU)

RAM

32GB Dual-Channel DDR5 262-pin SODIMM (5600MHz)

This laptop is purpose-built for developers and professionals who want a Kubuntu Linux-powered portable workstation and gaming platform. It features an Intel processor capable of hitting 5.4GHz and both integrated graphics and a dedicated NVIDIA 5070 Ti GPU for when you need extra power for machine learning or games.




Source link

Leave a Reply

Subscribe to Our Newsletter

Get our latest articles delivered straight to your inbox. No spam, we promise.

Recent Reviews


Google Maps has a long list of hidden (and sometimes, just underrated) features that help you navigate seamlessly. But I was not a big fan of using Google Maps for walking: that is, until I started using the right set of features that helped me navigate better.

Add layers to your map

See more information on the screen

Layers are an incredibly useful yet underrated feature that can be utilized for all modes of transport. These help add more details to your map beyond the default view, so you can plan your journey better.

To use layers, open your Google Maps app (Android, iPhone). Tap the layer icon on the upper right side (under your profile picture and nearby attractions options). You can switch your map type from default to satellite or terrain, and overlay your map with details, such as traffic, transit, biking, street view (perfect for walking), and 3D (Android)/raised buildings (iPhone) (for buildings). To turn off map details, go back to Layers and tap again on the details you want to disable.

In particular, adding a street view and 3D/raised buildings layer can help you gauge the terrain and get more information about the landscape, so you can avoid tricky paths and discover shortcuts.

Set up Live View

Just hold up your phone

A feature that can help you set out on walks with good navigation is Google Maps’ Live View. This lets you use augmented reality (AR) technology to see real-time navigation: beyond the directions you see on your map, you are able to see directions in your live view through your camera, overlaying instructions with your real view. This feature is very useful for travel and new areas, since it gives you navigational insights for walking that go beyond a 2D map.

To use Live View, search for a location on Google Maps, then tap “Directions.” Once the route appears, tap “Walk,” then tap “Live View” in the navigation options. You will be prompted to point your camera at things like buildings, stores, and signs around you, so Google Maps can analyze your surroundings and give you accurate directions.

Download maps offline

Google Maps without an internet connection

Whether you’re on a hiking trip in a low-connectivity area or want offline maps for your favorite walking destinations, having specific map routes downloaded can be a great help. Google Maps lets you download maps to your device while you’re connected to Wi-Fi or mobile data, and use them when your device is offline.

For Android, open Google Maps and search for a specific place or location. In the placesheet, swipe right, then tap More > Download offline map > Download. For iPhone, search for a location on Google Maps, then, at the bottom of your screen, tap the name or address of the place. Tap More > Download offline map > Download.

After you download an area, use Google Maps as you normally would. If you go offline, your offline maps will guide you to your destination as long as the entire route is within the offline map.

Enable Detailed Voice Guidance

Get better instructions

Voice guidance is a basic yet powerful navigation tool that can come in handy during walks in unfamiliar locations and can be used to ensure your journey is on the right path. To ensure guidance audio is enabled, go to your Google Maps profile (upper right corner), then tap Settings > Navigation > Sound and Voice. Here, tap “Unmute” on “Guidance Audio.”

Apart from this, you can also use Google Assistant to help you along your journey, asking questions about your destination, nearby sights, detours, additional stops, etc. To use this feature on iPhone, map a walking route to a destination, then tap the mic icon in the upper-right corner. For Android, you can also say “Hey Google” after mapping your destination to activate the assistant.

Voice guidance is handy for both new and old places, like when you’re running errands and need to navigate hands-free.

Add multiple stops

Keep your trip going

If you walk regularly to run errands, Google Maps has a simple yet effective feature that can help you plan your route in a better way. With Maps’ multiple stop feature, you can add several stops between your current and final destination to minimize any wasted time and unnecessary detours.

To add multiple stops on Google Maps, search for a destination, then tap “Directions.” Select the walking option, then click the three dots on top (next to “Your Location”), and tap “Edit Stops.” You can now add a stop by searching for it and tapping “Add Stop,” and swap the stops at your convenience. Repeat this process by tapping “Add Stops” until your route is complete, then tap “Start” to begin your journey.

You can add up to ten stops in a single route on both mobile and desktop, and use the journey for multiple modes (walking, driving, and cycling) except public transport and flights. I find this Google Maps feature to be an essential tool for travel to walkable cities, especially when I’m planning a route I am unfamiliar with.


More to discover

A new feature to keep an eye out for, especially if you use Google Maps for walking and cycling, is Google’s Gemini boost, which will allow you to navigate hands-free and get real-time information about your journey. This feature has been rolling out for both Android and iOS users.



Source link