Community Tutorials Linux Basics كيفية استخدام grep بفعالية على Linux
كيفية استخدام grep بفعالية على Linux
LINUX BASICS

كيفية استخدام grep بفعالية على Linux

SKYLINE Knowledge Base
Photo by Sigmund on Unsplash

دليل عملي خطوة بخطوة لـ كيفية استخدام grep بفعالية على Linux. أوامر مختبرة في الإنتاج، متطلبات مسبقة، تحقق نهائي وروابط لمواضيع ذات صلة.

grep هو أداة البحث اليومية على كل Unix. الصيغة الأساسية "أرني الأسطر التي تطابق هذا النمط" لكن مجموعة الأعلام (recursive, case-insensitive, fixed-string, سياق) تجعله مفيدًا.

المتطلبات المسبقة

  • shell على Linux/macOS/BSD.

الخطوة 1: المطابقة الافتراضية

grep ERROR /var/log/syslog
grep -i error /var/log/syslog
grep -F "1.2.3.4"
grep -w error
grep -v "^#"
grep -c ERROR /var/log/syslog

الخطوة 2: بحث recursive

grep -r "TODO" .
grep -rn "TODO" .
grep -rl "TODO" .
grep -r --include='*.php' "TODO" .
grep -r --exclude-dir='node_modules' --exclude-dir='.git' "TODO" .

الخطوة 3: أسطر السياق

grep -A 3 ERROR app.log
grep -B 3 ERROR app.log
grep -C 3 ERROR app.log

الخطوة 4: regex موسع

grep -E '" (4|5)[0-9]{2} ' access.log
grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}' file.txt
grep -Eo '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}' contacts.txt

الخطوة 5: أنماط متعددة

grep -e ERROR -e CRITICAL -e FATAL app.log
grep -E 'ERROR|CRITICAL|FATAL' app.log
grep -f patterns.txt app.log

الخطوة 6: مع find

find . -name '*.php' -mtime -7 -exec grep -Hn 'DB_PASSWORD' {} +
git ls-files '*.php' | xargs grep -ln '@deprecated'

الخطوة 7: ripgrep (rg)

sudo apt install -y ripgrep
rg TODO
rg -t php "TODO"
rg -e ERROR -A 3 -B 1

الخطوة 8: سطور مفيدة

grep -oE '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' /var/log/nginx/access.log \
  | sort | uniq -c | sort -nr | head -20

grep -rIc "TODO" . | grep -v ':0$' | sort -t: -k2 -nr | head -20

التحقق

grep --version | head -1
echo -e "alpha\nbeta\ngamma" | grep -c beta

الخاتمة

grep -rIn --include='*.X' PATTERN . يغطي 80% من احتياجات البحث.

الخطوات التالية

SKYLINE Engineering

@skyline

The engineering team at SKYLINE Industrial Solutions. We publish field-tested guides drawn from real KSA and GCC deployments.

See author profile
SKYLINE engineering services

Need this implemented for you?

Reading is free — building it right takes a team. SKYLINE engineers ship Linux Basics for Aramco vendors, banks, hospitals and government agencies across Saudi Arabia. Talk to us before you start.

Aramco Approved Contractor ISO 9001 · ISO 27001 SAMA CSF aligned NCA ECC ready 247+ KSA clients

Comments

0 total · 0 threads
Be the first to leave a comment.