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 *)
Both take the same shape: the thing to act on first, the destination last. If the destination is an existing directory, the file goes inside it. If it is a name that does not exist, that becomes the new name.

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.

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.

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.
This catches everyone once. Whether the destination already exists changes what the command means, and nothing in the line you typed tells you which case you are in. Run ls on the destination first — the same habit as lesson 6.
OptionOn cp and mvWorth using when
-iAsk before overwritingAlways, while learning
-rRecurse into directoriesRequired by cp for a folder
-vPrint each thing as it happensA large operation you want to watch
-nNever overwrite; skip insteadMerging one folder into another
-pKeep timestamps and permissionsCopying 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.

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

Preview / المعاينة

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

1. Why is there no separate rename command?

2. You run cp draft.txt final.txt and final.txt already existed. What happened to it?

3. Why does moving a folder to an external drive take time, when moving it on the same disk does not?

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