Lesson 7 / الدرس 7
Copying, moving and renaming / النسخ والنقل وإعادة التسمية
Two commands cover three jobs, because moving a file and renaming it turn out to be the same operation — and both will overwrite something without asking if you let them.
أمران يغطيان ثلاثة أعمال، لأن نقل الملف وإعادة تسميته يتبيّن أنهما العملية نفسها — وكلاهما يستبدل بشيء دون سؤال إن تركته.
cp notes.txt backup.txt copy a file
cp -r site site-backup copy a directory and its contents
mv notes.txt archive/ move it into a directory
mv notes.txt todo.txt rename it — the same command
mv site/*.html pages/ move several at once (lesson 12 explains the *)
Renaming is just moving
There is no rename command, and that is not an oversight. A file's name is an entry in a directory, so changing the name and changing which directory it is in are the same operation — you are editing that entry either way. mv does both, and it does them instantly, because nothing is copied: only the entry changes.
rename، وهذا ليس سهوًا. فاسم الملف مُدخَل في مجلد، فتغيير الاسم وتغيير المجلد الذي هو فيه العملية نفسها — فأنت تحرر ذلك المُدخَل في الحالتين. وmv يفعل كليهما، ويفعلهما فورًا، لأن لا شيء يُنسخ: بل يتغير المُدخَل فقط.That instant speed has one exception worth knowing: moving to a different disk is really a copy followed by a delete, because the entry cannot simply be re-filed onto other hardware. This is why moving a large folder to an external drive takes minutes while moving it across your own disk takes none.
cp draft.txt final.txt يدمّر ما كان final.txt، بلا تحذير ولا طريق عودة. أضف -i إلى أي منهما فيسأل أولًا. وفي أي شيء يحزنك فقدانه، اكتب -i.The trailing slash, and one subtle difference
cp -r site backup backup does not exist -> creates backup, a copy of site
cp -r site backup backup DOES exist -> creates backup/site
The same command. Two different results, decided by something you cannot see.
ls on the destination first — the same habit as lesson 6.
ls على الوجهة أولًا — العادة نفسها من الدرس 6.| Option | On cp and mv | Worth using when |
|---|---|---|
| -i | Ask before overwriting | Always, while learning |
| -r | Recurse into directories | Required by cp for a folder |
| -v | Print each thing as it happens | A large operation you want to watch |
| -n | Never overwrite; skip instead | Merging one folder into another |
| -p | Keep timestamps and permissions | Copying to a server |
mv needs no -r: it moves a directory as one entry, contents and all, because it is only re-filing the name.
mv لا يحتاج -r: فهو ينقل المجلد مُدخَلًا واحدًا بمحتواه كله، لأنه لا يفعل إلا إعادة فهرسة الاسم.rsync -av source/ dest/ أفضل من cp: فهو ينسخ ما اختلف فقط، فلا يكلّف تنفيذه مرتين شيئًا تقريبًا في الثانية. وانتبه للشَّرطة المائلة في آخر المصدر — فمعها تنسخ المحتوى، وبدونها تنسخ المجلد نفسه. وهذا الموقع يُنشر بذلك الأمر بالضبط.Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1.
Why is there no separate rename command?
rename منفصل؟Both edit the same directory entry, which is also why both are instant on one disk — nothing is copied.
2.
You run cp draft.txt final.txt and final.txt already existed. What happened to it?
cp draft.txt final.txt وfinal.txt كان موجودًا. ماذا حدث له؟No warning, no backup. -i is what makes it ask, and it is worth typing on anything that matters.
-i هو ما يجعله يسأل، ويستحق أن يُكتب في كل ما يهم.3. Why does moving a folder to an external drive take time, when moving it on the same disk does not?
On one disk only the directory entry changes. Across disks the data itself has to travel, so the same command is doing something quite different.
Score / النتيجة: 0 / 3
Your task / مهمتك
Make a folder with a few files, then demonstrate the trailing-slash trap yourself: copy a directory onto a name that does not exist, then copy it again onto one that does, and show what you ended up with each time. Present both sessions as a page and explain, in your own words, how to know in advance which of the two will happen.
اصنع مجلدًا فيه بضعة ملفات، ثم اعرض فخّ الوجهة بنفسك: انسخ مجلدًا إلى اسم غير موجود، ثم انسخه مرة أخرى إلى اسم موجود، وأظهر ما انتهيت إليه في كل مرة. واعرض الجلستين في صفحة واشرح بكلماتك كيف تعرف مسبقًا أي الحالتين ستقع.
- Both cases run for real, with the resulting listings الحالتان منفَّذتان فعلًا، مع القوائم الناتجة
- The two results are visibly different النتيجتان مختلفتان بوضوح
- Your own explanation of how to predict which you will get شرحك أنت لكيفية توقع أيهما ستحصل عليه