Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to grep string without filenames in Linux?
The grep command is a powerful tool for searching patterns in files. By default, when searching multiple files, grep displays the filename along with each matching line. However, sometimes you only want to see the matching lines without the filenames cluttering the output.
Let's see a simple example where we grep a pattern in multiple files in a directory.
Default grep Behavior with Multiple Files
grep -i 'lp' Sample*
Sample: The sample was printed via the internet. Sample: I believe lp cares about what the device is. Sample1: This was printed via the internet. Sample1: I believe lp cares about what the device is. Sample2: This was printed via the internet. Sample3: I believe lp cares about what the device is.
Notice that each matching line is prefixed with the filename (Sample:, Sample1:, etc.). While this is useful for identifying which file contains each match, sometimes you only need the actual matching text.
Using the -h Flag to Hide Filenames
The -h or --no-filename flag suppresses the filename prefix in the output. This gives you clean results showing only the matching lines.
grep -i -h 'lp' Sample*
The sample was printed via the internet. I believe lp cares about what the device is. This was printed via the internet. I believe lp cares about what the device is. This was printed via the internet. I believe lp cares about what the device is.
Combining with Line Numbers
You can combine -h with -n to show line numbers without filenames:
grep -i -h -n 'lp' Sample*
1:The sample was printed via the internet. 2:I believe lp cares about what the device is. 1:This was printed via the internet. 2:I believe lp cares about what the device is. 1:This was printed via the internet. 1:I believe lp cares about what the device is.
Key Points
The
-hflag is only effective when searching multiple filesWhen searching a single file, grep doesn't show filenames by default
This flag is particularly useful in scripts where you need clean output for further processing
You can combine
-hwith other grep options like-i(case-insensitive),-n(line numbers), or-v(invert match)
Common Use Cases
Log analysis − Extract error messages from multiple log files without filename clutter
Configuration searches − Find specific settings across multiple config files
Code searches − Look for function calls or variables across source files
Data extraction − Get clean output for piping to other commands or scripts
Conclusion
The -h flag in grep suppresses filename prefixes when searching multiple files, providing cleaner output that focuses on the matching content rather than file locations. This is essential for data processing workflows and when you need uncluttered results from pattern searches across multiple files.
