Introduction
Linux में जब हमें हजारों लाइनों के डेटा में से किसी विशेष शब्द, पैटर्न या जानकारी को खोजना होता है, तब grep (Global Regular Expression Print) कमांड का उपयोग किया जाता है।
Grep Linux के सबसे शक्तिशाली कमांड्स में से एक है और यह Ethical Hackers, Cyber Security Professionals, System Administrators और Developers द्वारा रोज़ाना उपयोग किया जाता है।
यदि आपने Kali Linux, Ubuntu या किसी अन्य Linux Distribution का उपयोग किया है, तो आपने किसी न किसी समय grep कमांड का जरूर उपयोग किया होगा।
What is Grep Command?
Grep एक Linux Utility है जो किसी फाइल या टेक्स्ट में दिए गए Pattern को खोजती है और उस Pattern से मेल खाने वाली लाइनों को प्रदर्शित करती है।
Basic Syntax
grep “PATTERN” filename.txt
Example
grep “admin” users.txt
यह कमांड users.txt फाइल में “admin” शब्द खोजेगी और उससे मेल खाने वाली सभी लाइनों को दिखाएगी।
Image: Basic Grep Command
📷 Screenshot Suggestion:
grep “root” /etc/passwd
और उसके नीचे आउटपुट दिखाएं।
The Grep Family
Linux में grep के दो महत्वपूर्ण संस्करण भी मौजूद हैं:
egrep
Extended Regular Expressions (ERE) को सपोर्ट करता है।
egrep “admin|root” users.txt
fgrep
Fixed String Search के लिए उपयोग किया जाता है।
fgrep “administrator” users.txt
Modern Alternative
आजकल egrep और fgrep की जगह grep के Flags का उपयोग किया जाता है।
egrep Equivalent
grep -E “admin|root” users.txt
fgrep Equivalent
grep -F “administrator” users.txt
Important Grep Flags
1. Recursive Search (-R)
किसी डायरेक्टरी और उसकी सभी सब-डायरेक्टरी में Pattern खोजता है।
grep -R “password” /home
Use Case
Pentesting के दौरान Passwords, API Keys या Sensitive Data खोजने के लिए।
2. Hide File Names (-h)
Recursive Search के दौरान File Names छुपा देता है।
grep -Rh “password” /home
3. Count Matches (-c)
Pattern कितनी बार मिला है, उसकी संख्या दिखाता है।
grep -c “root” users.txt
Example Output
5
4. Ignore Case (-i)
Uppercase और Lowercase दोनों को समान मानता है।
grep -i “admin” users.txt
यह खोजेगा:
- admin
- Admin
- ADMIN
- AdMiN
सभी को।
5. Show Only File Names (-l)
केवल उन फाइलों के नाम दिखाता है जिनमें Pattern मौजूद हो।
grep -Rl “password” .
6. Show Line Numbers (-n)
Pattern वाली लाइन के साथ उसका Line Number भी दिखाता है।
grep -n “admin” users.txt
Example Output
12: admin account found
25: admin login enabled
7. Invert Match (-v)
Pattern को छोड़कर बाकी सभी लाइनें दिखाता है।
grep -v “admin” users.txt
8. Extended Regex (-E)
Regular Expressions के साथ Search करने के लिए।
grep -E “admin|root|user” users.txt
9. Multiple Patterns (-e)
एक साथ कई Patterns खोजने के लिए।
grep -e “admin” -e “root” users.txt
Difference Between -E and -e
कई Linux Beginners इन दोनों Flags को लेकर Confuse हो जाते हैं।
-e Flag
Multiple Patterns को Search करने के लिए।
grep -e “admin” -e “root” file.txt
-E Flag
Extended Regular Expressions को Enable करता है।
grep -E “admin|root” file.txt
BRE vs ERE Explained
BRE (Basic Regular Expressions)
Simple Pattern Matching के लिए।
Example
grep “admin” users.txt
ERE (Extended Regular Expressions)
Advanced Pattern Matching के लिए।
Example
grep -E “^a.*n$” words.txt
यह उन सभी शब्दों को Match करेगा जो:
- a से शुरू होते हैं
- n पर समाप्त होते हैं
उदाहरण:
action
amazon
admin
Practical Examples for Ethical Hackers
Find Passwords
grep -Ri “password” .
Find API Keys
grep -Ri “apikey” .
Search SSH Keys
grep -Ri “ssh-rsa” .
Find Root User Entries
grep “root” /etc/passwd
Search Failed Login Attempts
grep “Failed password” /var/log/auth.log
Grep + Other Linux Commands
Grep with Cat
cat users.txt | grep “admin”
Grep with ps
ps aux | grep apache
Grep with netstat
netstat -antp | grep 80
Grep with ls
ls -la | grep “.txt”
Why Grep is Important in Cyber Security?
Cyber Security Professionals और Ethical Hackers grep का उपयोग करते हैं:
- Log Analysis
- Threat Hunting
- Password Discovery
- API Key Hunting
- Source Code Auditing
- Malware Investigation
- Incident Response
- Digital Forensics
के दौरान।
Conclusion
Grep Linux का एक बेहद शक्तिशाली Command-Line Tool है जो बड़ी मात्रा में डेटा में से आवश्यक जानकारी निकालने में मदद करता है। चाहे आप Linux Beginner हों, System Administrator हों या Ethical Hacker, grep Command को अच्छी तरह सीखना आपके Linux Skill Set को कई गुना बढ़ा सकता है।
यदि आप Cyber Security या Ethical Hacking सीख रहे हैं, तो Grep Command आपके सबसे महत्वपूर्ण हथियारों में से एक है।

