Lesson 12 / الدرس 12
Wildcards: naming many files at once / البدائل: تسمية ملفات كثيرة دفعة واحدة
A star in a filename is not passed to the command. The shell expands it into a list of real names first, and understanding that one fact explains several things that otherwise look like magic.
النجمة في اسم ملف لا تُمرَّر إلى الأمر. بل توسّعها الـshell إلى قائمة بأسماء حقيقية أولًا، وفهم تلك الحقيقة وحدها يفسّر أشياء عدة تبدو بغيرها سحرًا.
Type ls *.css and the command ls never sees a star. Before it runs, the shell looks in the directory, finds every name matching the pattern, and replaces the star with all of them. The command receives an ordinary list of filenames and has no idea a wildcard was involved. This is called globbing.
ls *.css فلا يرى الأمر ls نجمةً قط. فقبل تنفيذه تنظر الـshell في المجلد، وتجد كل اسم يطابق النمط، وتستبدل بالنجمة كل تلك الأسماء. فيتلقى الأمر قائمة عادية بأسماء ملفات ولا يدري أن ثمة بديلًا. ويسمى هذا التوسيع (globbing).You type: ls *.css
The shell expands: ls site.css tokens.css admin.css
ls receives: three ordinary filenames
What this lets you do
| Command | Does |
|---|---|
| cp *.html backup/ | Copy every HTML file into backup |
| rm *.tmp | Remove every temporary file here |
| mv report-2025-*.pdf archive/ | Move last year's reports |
| ls -l */index.html | The index of every subdirectory |
| grep "todo" *.php | Search every PHP file here |
This is the point of the terminal in one table. Every one of these is a single line that would be a long session of clicking, and it takes the same time for five files or five hundred.
A star does not cross a slash
*.css matches CSS files in the current directory only. It does not descend, because * never matches the / in a path. To reach one level down write */*.css, and for any depth most shells support **/*.css — though that has to be switched on in bash. When you want the whole tree, find from lesson 8 is the more reliable tool.
*.css يطابق ملفات CSS في المجلد الحالي فقط. ولا ينزل، لأن * لا تطابق / في مسار أبدًا. وللوصول مستوى أدنى اكتب */*.css، ولأي عمق تدعم معظم الـshells صيغة **/*.css — وإن وجب تشغيلها في bash. وحين تريد الشجرة كلها، فـfind من الدرس 8 هو الأداة الأوثق.rm *.txt وrm * .txt مسافة واحدة وكارثة. فالثاني يوسّع * إلى كل ملف في المجلد ثم يبحث عن ملف اسمه .txt؛ وحين يشكو من الملف المفقود يكون كل ما عداه قد ذهب. فقبل أي rm ببديل، مرّر النمط نفسه على ls واقرأ القائمة — فالـshell ستوسّعه بالطريقة نفسها تمامًا.
One more expansion, which is not a wildcard but sits beside them: braces generate a list rather than matching existing names. mkdir -p site/{css,js,img} creates three directories, and cp config.php{,.bak} expands to cp config.php config.php.bak — a compact way to take a backup before editing something.
mkdir -p site/{css,js,img} ينشئ ثلاثة مجلدات، وcp config.php{,.bak} يتوسّع إلى cp config.php config.php.bak — وهي طريقة مختصرة لأخذ نسخة احتياطية قبل تعديل شيء.echo أمامه: فـecho rm *.txt يطبع الأمر الذي كانت الـshell ستنفّذه، ولا ينفّذ شيئًا. وهو أرخص فحص أمان في هذه الدورة ويعمل مع كل أمر.Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1.
Who expands *.css into a list of filenames?
*.css إلى قائمة أسماء ملفات؟The command receives ordinary filenames and never knows a wildcard was typed. Every other behaviour in this lesson follows from that.
2.
Why does ls *.xyz say that *.xyz does not exist?
ls *.xyz إن *.xyz غير موجود؟A failed expansion hands the command the literal text. The confusing message is actually telling you that nothing matched.
3. What is the safe way to check a wildcard before deleting with it?
The shell expands the pattern identically whatever command follows it, so ls shows you exactly the list rm would have received.
ls بالضبط القائمة التي كان rm سيتلقاها.Score / النتيجة: 0 / 3
Your task / مهمتك
Make a folder with at least a dozen files of several different types and names. Then write a page showing five wildcard patterns you invented, what each one expanded to (use echo to find out), and one operation you performed with one of them. Include one pattern that deliberately matches nothing, and explain the message you got.
اصنع مجلدًا فيه اثنا عشر ملفًا على الأقل من أنواع وأسماء مختلفة. ثم اكتب صفحة تُظهر خمسة أنماط بدائل اخترعتها، وما توسّع إليه كل واحد (استخدم echo لتعرف)، وعملية واحدة نفّذتها بأحدها. وأدرج نمطًا واحدًا لا يطابق شيئًا عمدًا، واشرح الرسالة التي حصلت عليها.
- Five patterns of your own, with what each expanded to خمسة أنماط من عندك، مع ما توسّع إليه كل واحد
- At least one uses ? or [ ], not only * واحد على الأقل يستخدم ? أو [ ]، لا * فقط
- One pattern that matches nothing, and the message explained نمط لا يطابق شيئًا، والرسالة مشروحة
- One real operation performed with a wildcard عملية حقيقية واحدة نُفّذت ببديل