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.

You type:          ls *.css
The shell expands:  ls site.css tokens.css admin.css
ls receives:        three ordinary filenames
Everything about wildcards follows from this. Run the demonstration for the three patterns and what each matches — and note the consequence in the last panel, which is the part people find surprising.

What this lets you do

CommandDoes
cp *.html backup/Copy every HTML file into backup
rm *.tmpRemove every temporary file here
mv report-2025-*.pdf archive/Move last year's reports
ls -l */index.htmlThe index of every subdirectory
grep "todo" *.phpSearch 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.

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.

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

Preview / المعاينة

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

1. Who expands *.css into a list of filenames?

2. Why does ls *.xyz say that *.xyz does not exist?

3. What is the safe way to check a wildcard before deleting with it?

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 عملية حقيقية واحدة نُفّذت ببديل
How do you want to submit? / كيف تريد التسليم؟