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 "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
Four options carry almost all the value: -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.

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.

Patterns, not just words

PatternMatchesReads as
^bodybody only at the start of a linebegins with
;$a semicolon at the end of a lineends with
colou?rcolor and colourthe u is optional
[0-9]+one or more digitsa number
error|fataleither wordor
.any single characteranything

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 |.

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.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. Which combination gives you file names and line numbers across a whole project?

2. Why must a grep pattern be quoted?

3. What does ^body match?

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 كل نمط مقتبَس
How do you want to submit? / كيف تريد التسليم؟