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


Smartphones have amazing cameras, but I’m not happy with any of them out of the box. I have to tweak a few things. If you have a Samsung Galaxy phone, these settings won’t magically transform your main camera into an entirely new piece of hardware, but it can put you in a position to capture the best photos your phone can muster.

Turn on the composition guide

Alignment is easier when you can see lines

Grid lines visible using the composition guide feature in the Galaxy Z Fold 6 camera app. Credit: Bertel King / How-To Geek

Much of what makes a good photo has little to do with how many megapixels your phone puts out. It’s all about the fundamentals, like how you compose a shot. One of the most important aspects is the placement of your subject.

Whether you’re taking a picture of a person, a pet, a product, or a plant, placement is everything. Is the photo actually centered? Or, if you’re trying to cultivate more visual interest, are you adhering to the rule of thirds (which is not to suggest that the rule of thirds is an end-all, be-all)? In either case, having an on-screen grid makes all the difference.

To turn on the grid, tap on the menu icon and select the settings cog. Then scroll down until you see Composition guide and tap the toggle to turn it on.

Going forward, whenever you open your camera, you will see a Tic Tac Toe-shaped grid on your screen. Now, instead of merely raising your phone and snapping the shot, take the time to make sure everything is aligned.

Take advantage of your camera’s max resolution

Having more pixels means you can capture more detail

I have a Samsung Galaxy Z Fold 6. The camera hardware on my book-style foldable phone is identical to that of the Galaxy S24 released in the same year, which hasn’t changed much for the Galaxy S25 or the Galaxy S26 released since. On each of these phones, however, the camera app isn’t taking advantage of the full 50MP that the main lens can produce. Instead, photos are binned down to 12MP. The same thing happens even if you have the 200MP camera found on the Galaxy S26 Ultra and the Galaxy Z Fold 7.

To take photos at the maximum resolution, open the camera app and look for the words “12M” written at either the top or side of your phone, depending on how you’re holding it. The numbers will appear right next to the indicator that toggles whether your flash is on or off. For me, tapping here changes the text from 12M to 50M.

Photo resolution toggle in the camera app of a Samsung Galaxy Z Fold 6. Credit: Bertel King / How-To Geek

But wait, we aren’t done yet. To save storage, your phone may revert back to 12MP once you’re done using the app. After all, 12MP is generally enough for most quick snaps and looks just fine on social media, along with other benefits that come from binning photos. But if you want to know that your photos will remain at a higher resolution when you open the camera app, return to camera settings like we did to enable the composition guide, then scroll down until you see Settings to keep. From there, select High picture resolutions.

Use volume keys to zoom in and out

Less reason to move your thumb away from the shutter button

Using volume keys to zoom in the camera app on a Samsung Galaxy Z Fold 6. Credit: Bertel King / How-To Geek

Our phones come with the camera icon saved as one of the favorites we see at the bottom of the homescreen. I immediately get rid of this icon. When I want to take a photo, I double-tap the power button instead.

Physical buttons come in handy once the app is open as well. By default, pressing the volume keys will snap a photo. Personally, I just tap the shutter button on the screen, since my thumb hovers there anyway. In that case, what’s something else the volume keys can do? I like for them to control zoom. I don’t zoom often enough to remember whether my gesture or swipe will zoom in or out, and I tend to overshoot the level of zoom I want. By assigning this to the volume keys, I get a more predictable and precise degree of control.

To zoom in and out with the volume keys, open the camera settings and select Shooting methods > Press Volume buttons to. From here, you can change “Take picture or record video” to “Zoom in or out.”

Adjust exposure

Brighten up a photo before you take it

Exposure setting in the camera app on a Samsung Galaxy Z Fold 6. Credit: Bertel King / How-To Geek

The most important aspect of a photo is how much light your lens is able to take in. If there’s too much light, your photo is washed out. If there isn’t enough light, then you don’t have a photo at all.

Exposure allows you to adjust how much light you expose to your phone’s image sensor. If you can see that a window in the background is so bright that none of the details are coming through, you can turn down the exposure. If a photo is so dark you can’t make out the subject, try turning the exposure up. Exposure isn’t a miracle worker—there’s no making up for the benefits of having proper lighting, but knowing how to adjust exposure can help you eke out a usable shot when you wouldn’t have otherwise.

To access exposure, tap the menu button, then tap the icon that looks like a plus and a minus symbol inside of a circle.

From this point, you can scroll up and down (or side to side, if holding the phone vertically) to increase or decrease exposure. If you really want to get creative, you can turn your photography up a notch by learning how to take long exposure shots on your Galaxy phone.


Help your camera succeed

Will changing these settings suddenly turn all of your photos into the perfect shot? No. No camera can do that, even if you spend thousands of dollars to buy it. But frankly, I take most of my photos for How-To Geek using my phone, and these settings help me get the job done.

Samsung Galaxy Z Fold 7 on a white background.

Brand

Samsung

RAM

12GB

Storage

256GB

Battery

4,400mAh

Operating System

One UI 8

Connectivity

5G, LTE, Wi-Fi 7, Bluetooth 5.4

Samsung’s thinnest and lightest Fold yet feels like a regular phone when closed and a powerful multitasking machine when open. With a brighter 8-inch display and on-device Galaxy AI, it’s ready for work, play, and everything in between.




Source link