The Destination Rule, and Overwriting

One rule explains most of the confusion. What cp and mv do depends on whether the destination already exists as a directory.

If the destination is an existing directory, the source goes inside it, keeping its name.

If the destination is not, it is treated as the new name.

So mv a.txt archive moves a.txt into the archive directory if archive exists, and renames a.txt to archive if it does not.

The same command, two outcomes

DIRECTORY EXISTS

mv notes.txt archive

when archive/ EXISTS:
  -> archive/notes.txt

IT DOES NOT

mv notes.txt archive

when archive/ does NOT exist:
  -> a file named archive
Compare → This is why people trail a slash on the destination when they mean a directory. Writing mv notes.txt archive/ makes the intent explicit, and errors instead of silently creating a strangely named file.

✦ Overwriting happens without asking

If the destination file already exists, both cp and mv replace it silently. There is no prompt and no backup. Passing -i makes them ask first, and many people alias cp and mv to always include it. Until you do, ls the destination before you overwrite something you cannot recreate.

SANKOFA SANDBOX
learner:~$

Try it → Run `mv readme.txt projects` and see where it went. Reset, then run `mv readme.txt notthere` and see what you get instead. The difference is entirely whether the destination already existed.

◈ Graded Task

Reorganize the workspace. Create a directory called backup and copy the whole archive project into it, contents included. Then rename the logs directory to archive-logs. Leave the original archive project exactly where it is.

Checked against the filesystem you leave behind, not the commands you type, so any route that gets the job done counts. 5 visible and 2 hidden checks.

SANKOFA SANDBOX
learner:~$

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Practice Test →
← Back