Linux Labs

First steps / Stage 7 of 15

Creating and organising files

Not running Starting usually ~20 s
Leave the lesson

Step 4 of 10 · theory · 5 min

Moving and renaming

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.

Check yourself 1 question

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 cp does. After mv, the old name is gone.

  • The slash is a safeguard for when the directory does not exist. When old exists, mv report.txt old and mv report.txt old/ do the same thing.

  • An existing directory as the destination means “put it inside”. mv cannot replace a directory with a file — check with ls -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.
  • cp and mv silently replace an existing file at the destination; -i asks first.
  • A trailing slash on the destination demands a directory: if there is none, the command fails and the file keeps its name.
Ubuntu 24.04
Loading…