Lesson 6 / الدرس 6

Creating and removing / الإنشاء والحذف

Making a file or a folder takes one short command. Removing one takes another — and that second command deserves more respect than any other in this course, because there is no undo.

إنشاء ملف أو مجلد يحتاج أمرًا قصيرًا واحدًا. وحذفه يحتاج آخر — وذلك الأمر الثاني يستحق احترامًا أكثر من أي أمر آخر في هذه الدورة، لأنه لا تراجع.

mkdir notes                 make a directory
mkdir -p site/assets/css    make the whole chain, without complaining
touch notes/todo.txt        make an empty file (or update its timestamp)
rmdir notes                 remove a directory — only if it is empty
rm notes/todo.txt           remove a file
rm -r site                  remove a directory and everything in it
Six commands, and the last one is the dangerous member of the family. Note mkdir -p: without it, making site/assets/css fails unless site/assets already exists.

rm has no recycle bin

When you delete a file in a file manager it moves to a bin and you can change your mind. rm does not do that. It unlinks the file and the space becomes available for anything else to use. There is no command that undoes it, and recovery — if it is possible at all — is a specialist job on a disk you have stopped writing to.

$ ls old-drafts          look first
draft-1.txt  draft-2.txt
$ rm -r old-drafts       then delete exactly that
The habit, in two lines. It costs a second and it is the difference between deleting the folder you meant and the one whose name you mistyped. Run the demonstration for the two flags worth knowing and what each of them actually does.

Why rmdir still exists

rmdir removes a directory only when it is already empty, and refuses otherwise. That refusal is a feature: it is impossible to destroy anything you had forgotten was in there. When you mean "remove this empty folder", using rmdir instead of rm -r means a mistake becomes an error message rather than a loss.

touch is the other command with a second job. On a file that does not exist it creates an empty one. On a file that does exist it changes the modification time and leaves the contents alone — which sounds pointless until you meet a build tool that decides what to rebuild by comparing timestamps.

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

Preview / المعاينة

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

1. What does mkdir -p site/assets/css do that mkdir alone does not?

2. Why prefer rmdir when you mean to remove an empty directory?

3. What does -f add to rm?

Your task / مهمتك

Build a small project skeleton from the terminal alone — at least three nested directories and four files — using mkdir -p and touch. Then remove one part of it safely, showing the ls you ran first. Present the whole session as a page, and finish with the rule you will follow before every future rm, in your own words.

ابنِ هيكل مشروع صغير من الطرفية وحدها — ثلاثة مجلدات متداخلة وأربعة ملفات على الأقل — مستخدمًا mkdir -p وtouch. ثم احذف جزءًا منه بأمان، مُظهرًا ls الذي نفّذته أولًا. واعرض الجلسة كلها في صفحة، واختم بالقاعدة التي ستتبعها قبل كل rm مستقبلًا بكلماتك.

  • A real session, with mkdir -p and touch جلسة حقيقية فيها mkdir -p وtouch
  • The ls you ran before deleting anything الـls الذي نفّذته قبل حذف أي شيء
  • A tree or listing of what you ended up with شجرة أو قائمة بما انتهيت إليه
  • Your own rule for before every rm قاعدتك أنت لما قبل كل rm
How do you want to submit? / كيف تريد التسليم؟