Lesson 8 / الدرس 8

Finding files / العثور على الملفات

You know a file exists and not where it is. One command searches the tree by name, size or age — and it is the first command in this course whose arguments read like a sentence.

تعرف أن ملفًا موجود ولا تعرف أين. وأمر واحد يبحث في الشجرة بالاسم أو الحجم أو العمر — وهو أول أمر في هذه الدورة تُقرأ وُسَطاؤه كأنها جملة.

find takes a place to start and then a description of what you are looking for, and it walks everything underneath. Unlike the search box in a file manager it looks at the actual tree every time, so it never gives you a stale answer from an index built last week.

find . -name "*.css"              CSS files, from here down
find . -iname "readme*"            case-insensitive
find . -type d -name "assets"      directories only
find . -type f -size +10M          files over ten megabytes
find . -mtime -7                   changed in the last seven days
find . -name "*.log" -delete       found, and removed
The pattern is always the same: where to look, then one or more tests. Note the quotation marks around *.css — lesson 15 explains why leaving them off gives a different and confusing result.

The tests worth knowing

TestMatchesExample
-name "x"Name matches exactly, wildcards allowed-name "*.jpg"
-iname "x"The same, ignoring capitals-iname "*.JPG"
-type fOrdinary files only-type f -name "*.php"
-type dDirectories only-type d -name "node_modules"
-size +10MLarger than ten megabytes-size +100M
-mtime -7Modified fewer than seven days ago-mtime -1
-emptyEmpty files and directories-type d -empty

Tests combine, and listing two means both must be true. The signs matter: +10M is larger than, -10M is smaller, and a bare 10M means exactly ten.

find . -type f -size +10M -mtime -30
Read it as a sentence: from here, ordinary files, bigger than ten megabytes, changed in the last thirty days. That is the question "what recently made my disk full?" — and it is the shape most useful find commands take. Run the demonstration for three you will genuinely reuse.

Finding a command rather than a file

A different question: not "where is my file" but "where is the program that runs when I type this". which php prints the path of the php that would run, and type tells you more — whether it is a program, a built-in, or a shortcut you defined yourself. This becomes important the moment you have two versions of something installed, which lessons 17 and 20 come back to.

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

Preview / المعاينة

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

1. What does find . -type d -name "assets" look for?

2. Before adding -delete to a find command, what should you do?

3. You type php and get an unexpected version. Which command tells you which one runs?

Your task / مهمتك

Answer three real questions about your own machine with find: what are the five largest files in your home directory, what did you change in the last two days, and where is every copy of some file type you care about. Show the exact command and a trimmed version of what came back, and say what surprised you.

أجب ثلاثة أسئلة حقيقية عن جهازك بـfind: ما أكبر خمسة ملفات في مجلد منزلك، وما الذي غيّرته في آخر يومين، وأين كل نسخة من نوع ملفات يهمك. وأظهر الأمر بالضبط ونسخة مختصرة مما رجع، وقل ما الذي فاجأك.

  • Three real commands, run on your own machine ثلاثة أوامر حقيقية منفَّذة على جهازك
  • The output of each, trimmed but genuine مخرَج كل واحد، مختصرًا لكن حقيقيًا
  • A sentence on what surprised you جملة عمّا فاجأك
How do you want to submit? / كيف تريد التسليم؟