الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

grep

Search files or command output for lines that match a pattern (text or regular expression).

المستوى
امتحان RHCSA
المجموعة
النصوص والبحث
يحتاج root
لا
الحزمة
grep
يُسمّى أيضًا
egrep, fgrep

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

grep prints every line that contains a match. It is the main tool to search logs, config files and command output. The pattern is a regular expression: basic (default), extended (-E), fixed text (-F) or Perl style (-P).

egrep and fgrep are old names for grep -E and grep -F.

Exit codes: 0 = something matched, 1 = nothing matched, 2 = an error. Scripts use this with -q.

طريقة الكتابة

grep [OPTION...] PATTERNS [FILE...]
grep [OPTION...] -e PATTERNS ... [FILE...]
grep [OPTION...] -f PATTERN_FILE ... [FILE...]

الخيارات 48

الأكثر استخدامًا

Pattern type

-E, --extended-regexp
Extended regex: + ? {} | () work without backslashes.
-F, --fixed-strings
The pattern is plain text, not a regex (fast, no special characters).
-G, --basic-regexp
Basic regex (the default).
-P, --perl-regexp
Perl regex (like \d, lookahead).
-e, --regexp=pattern
A pattern; use it several times for several patterns.
-f, --file=file
Read patterns from a file, one per line.

Matching

-i, --ignore-case
Ignore upper/lower case.
--no-ignore-case
Do not ignore case (the default).
-v, --invert-match
Print lines that do NOT match.
-w, --word-regexp
Match whole words only.
-x, --line-regexp
Match whole lines only.
-y
Old name of -i.

Output

-c, --count
Print only the number of matching lines.
-l, --files-with-matches
Print only the names of files with a match.
-L, --files-without-match
Print the names of files without a match.
-n, --line-number
Print the line number before each line.
-o, --only-matching
Print only the matching part of the line.
-q, --quiet, --silent
Print nothing; only the exit code tells the result.
-s, --no-messages
Hide error messages about missing or unreadable files.
-m, --max-count=num
Stop after NUM matching lines.
-H, --with-filename
Print the file name with each line.
-h, --no-filename
Do not print file names.
-b, --byte-offset
Print the byte position of each line.
--label=label
The name to show for standard input.
-T, --initial-tab
Line up the output with a tab.
-Z, --null
Print a NUL after file names.
--color[=<when>], --colour[=<when>]
Highlight matches: never, always, auto.
--line-buffered
Print each line at once (for live pipes with tail -f).

Context

-A, --after-context=num
Also print NUM lines after each match.
-B, --before-context=num
Also print NUM lines before each match.
-C, --context=num, -num
Print NUM lines before and after.
--group-separator=sep
Text between context groups (default --).
--no-group-separator
No text between context groups.

Files and folders

-r, --recursive
Search all files in folders.
-R, --dereference-recursive
Like -r, and follow all symbolic links.
--include=glob
Search only files that match, like --include='*.conf'.
--exclude=glob
Skip files that match.
--exclude-from=file
Skip files that match patterns in a file.
--exclude-dir=glob
Skip folders that match, like .git.
-d, --directories=action
What to do with folders: read, skip, recurse.
-D, --devices=action
What to do with devices, pipes and sockets: read, skip.
-a, --text
Search binary files as if they were text.
-I
Skip binary files.
--binary-files=type
binary, text or without-match.
-U, --binary
Do not remove CR characters (Windows files).
-z, --null-data
Lines end with NUL.
-V, --version
Show the version.
--help
Show help.

أمثلة 14

$ مستخدم عادي# بصلاحية root (أو مع sudo)

  1. 1Find a user in the passwd file

    grep student /etc/passwd

    Every line that contains "student".

  2. 2Search a log, ignore case

    grep -i error /var/log/messages

    Finds Error, ERROR and error.

  3. 3Search all files in a folder, with line numbers

    grep -rn "PermitRootLogin" /etc/ssh/

    File name, line number and the line.

  4. 4Show the active settings (no comments, no empty lines)

    grep -Ev '^\s*(#|$)' /etc/ssh/sshd_config

    One command instead of two greps.

  5. 5Count matches

    grep -c "Failed password" /var/log/secure

    Number of failed SSH logins in the file.

  6. 6Search for several words

    grep -E 'error|fail|denied' /var/log/messages
    grep -e error -e fail /var/log/messages

    Two ways to search for "this OR that".

  7. 7Show lines around a match

    grep -B3 -A5 "Out of memory" /var/log/messages

    Context shows what happened before and after.

  8. 8Only list the files that contain the text

    grep -rl "192.168.122.10" /etc 2>/dev/null

    Find every config that still uses an old IP address.

  9. 9Print only the matching part (extract IPs)

    grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}' /var/log/secure | sort | uniq -c | sort -rn | head

    The IP addresses that appear most in the log.

  10. 10Whole words only

    grep -w root /etc/passwd

    Matches "root" but not "chroot".

  11. 11Use grep in an if test

    if grep -q '^student:' /etc/passwd; then echo "user exists"; fi

    -q prints nothing; the exit code decides.

  12. 12Search only some file types

    grep -rn --include='*.yaml' --exclude-dir=.git "image:" .

    All container images used in a repo.

  13. 13Find secrets before a commit

    grep -rniE '(password|secret|api[_-]?key)\s*[:=]' --exclude-dir=.git .

    A quick check; real projects use tools like gitleaks.

  14. 14Search compressed rotated logs

    zgrep "Accepted publickey" /var/log/secure-*.gz

    Same options as grep, on .gz files.

انتبه

  • Put the pattern in single quotes, or the shell may change *, $, \ or | first.
  • grep pattern file > file empties the file. Write to a new file.
  • A dot means "any character": use \. or -F for a real dot (IP addresses!).

نصائح للحفظ

  • i = ignore case, v = invert, r = recursive, n = numbers, c = count, l = list files, w = word, E = extended, o = only match.

الدليل الكامل

هذه الصفحة تشرح الأمر بكلمات بسيطة. الدليل الأصلي فيه كل التفاصيل.

في الطرفية: man grep