Lesson 9 / الدرس 9
Searching inside files with grep / البحث داخل الملفات بـgrep
The previous lesson found files by their names. This one finds them by what is written inside them — which is how you answer "where on earth is that string coming from?"
الدرس السابق وجد الملفات بأسمائها. وهذا يجدها بما هو مكتوب بداخلها — وهكذا تجيب سؤال "من أين يأتي هذا النص أصلًا؟"
grep takes something to look for and somewhere to look, and prints every line that matches. That is all it does, and it is probably the command you will use most often for the rest of your working life — because "which file contains this?" is the question you ask every time you inherit code you did not write.
grep شيئًا يبحث عنه وموضعًا يبحث فيه، ويطبع كل سطر مطابق. وهذا كل ما يفعله، وهو غالبًا الأمر الذي ستستخدمه أكثر من غيره بقية حياتك العملية — لأن "أي ملف يحوي هذا؟" هو السؤال الذي تطرحه كلما ورثت شيفرة لم تكتبها.grep "body" style.css lines containing body, in one file
grep -r "getUser" . search every file below here
grep -ri "todo" . the same, ignoring capitals
grep -rn "getUser" . with line numbers
grep -rl "getUser" . just the file names, not the lines
grep -rv "debug" config.php lines that do NOT contain debug
-r to search a whole tree, -i to ignore capitals, -n to say which line, and -l when you only want to know which files. -r للبحث في شجرة كاملة، و-i لتجاهل حالة الأحرف، و-n ليقول أي سطر، و-l حين لا تريد إلا معرفة أي الملفات.The one you will type most
$ grep -rn "course-terminal" public/
public/assets/css/tokens.css:66: --course-terminal: #0eb37c;
public/assets/css/tokens.css:168: --course-terminal: #046c44;
public/assets/css/site.css:379:[data-course="terminal"] { --course-grad: ...
grep -rn is the combination worth building into your fingers. Every result is a file, a line number and the line itself — enough to open the right place immediately. This is a real search of this site's own stylesheets. Run the demonstration to see how to read the output.
grep -rn هو التركيب الجدير بأن يدخل في أصابعك. فكل نتيجة ملف ورقم سطر والسطر نفسه — ما يكفي لفتح الموضع الصحيح فورًا. وهذا بحث حقيقي في ملفات تنسيق هذا الموقع. شغّل العرض لترى كيف تقرأ المخرَج.Patterns, not just words
| Pattern | Matches | Reads as |
|---|---|---|
| ^body | body only at the start of a line | begins with |
| ;$ | a semicolon at the end of a line | ends with |
| colou?r | color and colour | the u is optional |
| [0-9]+ | one or more digits | a number |
| error|fatal | either word | or |
| . | any single character | anything |
These are regular expressions, a small language for describing text that turns up in editors, in programming languages and in most search boxes worth using. Use grep -E for the ones above with + and |.
grep -E للأنماط أعلاه التي فيها + و|.grep — فتصير * قائمة بأسماء ملفاتك و$ متغيّرًا — فيبحث الأمر عن شيء لم تكتبه قط. والدرس 15 يشرح الآلية؛ والعادة التي تبنيها الآن أن نمط grep يكون بين علامتي اقتباس دائمًا.
Two more options earn their place. -c counts matches instead of printing them, which turns grep into a quick measuring tool — how many errors are in this log? And -C 3 prints three lines of context either side of each match, which is usually what you want when the matching line alone does not tell you enough.
-c يعدّ المطابقات بدل طباعتها، فيحوّل grep إلى أداة قياس سريعة — كم خطأً في هذا السجل؟ و-C 3 يطبع ثلاثة أسطر من السياق على كل جانب، وهو ما تريده عادةً حين لا يكفيك السطر المطابق وحده.grep -rn "getUser" . --exclude-dir=node_modules --exclude-dir=.git. فهذان المجلدان هما المتهمان المعتادان خلف بحث يرجع بعشرة آلاف سطر من شيفرة لم يكتبها أحد.Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1. Which combination gives you file names and line numbers across a whole project?
-r walks the tree and -n adds the line number. The result is file:line:text, which every editor can jump to.
-r في الشجرة ويضيف -n رقم السطر. والنتيجة file:line:text، ويستطيع كل محرر القفز إليها.2. Why must a grep pattern be quoted?
The shell expands the line before the command runs. Quoting is what hands the pattern to grep unchanged — lesson 15 goes through it properly.
3.
What does ^body match?
^body؟^ anchors to the start of a line and $ to the end. In a stylesheet, that is the difference between the selector and a mention of it.
^ على بداية السطر و$ على نهايته. وفي ملف تنسيق، هذا هو الفرق بين المُحدِّد وذكره.Score / النتيجة: 0 / 3
Your task / مهمتك
Take a real project on your machine — one of your own from the HTML or CSS course will do — and use grep to answer three questions about it: where a particular class or function is used, how many times some word appears, and which files mention something you were thinking of changing. Show each command and its output, and use at least one pattern with ^ or $.
خذ مشروعًا حقيقيًا على جهازك — واحد من مشاريعك في دورة HTML أو CSS يفي بالغرض — واستخدم grep للإجابة عن ثلاثة أسئلة عنه: أين يُستخدم صنف أو دالة بعينها، وكم مرة تظهر كلمة ما، وأي الملفات تذكر شيئًا كنت تفكر في تغييره. وأظهر كل أمر ومخرَجه، واستخدم نمطًا واحدًا على الأقل فيه ^ أو $.
- Three real searches on a real project ثلاث عمليات بحث حقيقية على مشروع حقيقي
- At least one uses -rn, and one uses -c واحدة على الأقل تستخدم -rn، وأخرى -c
- At least one pattern anchored with ^ or $ نمط واحد على الأقل مثبَّت بـ^ أو $
- Every pattern is quoted كل نمط مقتبَس