Moving and renaming are done by the same command. It also explains where the first draft went.
mv moves a file: once it runs, the old name is gone. mv reads the destination by the same table as cp. A directory: the file goes inside. A name that does not exist: the file takes that name. An existing file: it gets replaced, without a question.
That makes renaming the same mv within one directory: mv notes.txt README.txt. With a directory as the destination it is a move: mv README.txt handoff/. mv moves directories the same way, no extra options needed.
Now the drafts. There was no archive directory, so for the first mv archive was a new name — the first row of the table. For the second one a file already had that name — the third row, and mv replaced it without a question. The first draft's text is lost.
The protection is the same as for cp: create the directory first, write the destination with a trailing slash — mv draft1.txt archive/ — and confirm replacements with -i.
The current directory holds a file report.txt and a directory old. What does ls show after mv report.txt old?
-
A copy with the source left in place is what
cpdoes. Aftermv, the old name is gone. -
The slash is a safeguard for when the directory does not exist. When old exists,
mv report.txt oldandmv report.txt old/do the same thing. -
An existing directory as the destination means “put it inside”.
mvcannot replace a directory with a file — check withls -l old.
The destination old is an existing directory, so mv moves report.txt inside under the same name. Check ls old: report.txt is there.
Key points
-
Renaming and moving are one command,
mv: the destination decides which. -
cpandmvsilently replace an existing file at the destination;-iasks first. - A trailing slash on the destination demands a directory: if there is none, the command fails and the file keeps its name.