Read

Module 2 · Files and directories

Moving and renaming with mv

Why renaming and moving are the same operation, how mv decides between them, and the silent overwrite it shares with cp.

What you will learn

  • Rename a file or a directory in place.
  • Move one or several items into another directory.
  • Protect existing files with `-i` and `-n`.

Linux has no separate *rename* command, and it does not need one. A file's name is just an entry in a directory pointing at the data, and a file's location is just which directory holds that entry. Changing either is the same operation: take the entry out of one place and put it in another. mv — *move* — does exactly that. On the same filesystem it does not copy a single byte of content, which is why moving a 4 GB video is instant.

~% cd /root/lab/l14
l14% ls -F
2023/  a.jpg  archive/  b.jpg  c.jpg  draft.txt  photos/
l14% mv draft.txt report.txt
l14% mv a.jpg b.jpg c.jpg photos/
l14% mv 2023 archive/
l14% ls -F
archive/  photos/  report.txt
l14% ls archive photos
archive:
2023

photos:
a.jpg  b.jpg  c.jpg

How mv reads its last argument

Like cp, everything depends on the destination. If it is an existing directory, the sources go *inside* it and keep their names. If it is not, the single source is renamed to it. With several sources, the destination must be an existing directory. Unlike cp, directories need no -r: moving a directory moves the entry, and everything below it comes along for free.

CommandSituationResult
mv draft.txt report.txtreport.txt does not existRenamed.
mv report.txt archive/archive is a directoryNow archive/report.txt.
mv a.jpg b.jpg photos/Several sourcesBoth moved into photos.
mv 2023 archive/2023 is a directoryMoved with all its contents.
mv notes.txt report.txtreport.txt already existsreport.txt is replaced.

The silent overwrite

The last row is the dangerous one. Renaming onto a name that is taken throws away the old file without a word, and there is no undo. Three options change that: -i asks overwrite 'report.txt'? and waits for y or n; -n never overwrites and quietly skips; -f forces the overwrite without asking, which is already the default here. -v reports every move as 'x' -> 'dest/x', handy when a wildcard moves many files at once (it works in this VM even though mv --help does not list it).

The same trailing-slash habit from cp applies. mv report.txt archive/ fails if archive is not a directory, while mv report.txt archve with a typo happily renames your file to archve and reports nothing. And when a name starts with a dash, use -- (mv -- -old.txt old.txt) so it is not read as an option.

Commands in this lesson

CommandWhat it does
mv OLD NEWRename a file or directory.
mv SRC... DIR/Move one or more items into a directory.
mv -i SRC DESTAsk before overwriting.
mv -n SRC DESTNever overwrite; skip silently.
mv -v SRC... DIR/Print each move as it happens.

Quiz

  1. Which command renames `old.txt` to `new.txt`?

    • `rename old.txt new.txt`
    • `mv old.txt new.txt`
    • `cp -r old.txt new.txt`
  2. Why is moving a huge file to another directory on the same disk instant?

    • `mv` compresses it first
    • Only the directory entry changes; the data is not copied
    • `mv` copies in the background
  3. Does moving a directory with `mv` need `-r`?

    • Yes, like `cp`
    • No, the whole tree moves with its entry
    • Only if the directory is not empty
  4. `mv notes.txt report.txt` and `report.txt` already exists. What happens by default?

    • `report.txt` is replaced without asking
    • `mv` refuses with `File exists`
    • The two files are merged
  5. You meant `mv report.txt archive` but typed `mv report.txt archve`. No directory `archve` exists. What happened?

    • An error: no such directory
    • The file was renamed to `archve`
    • A directory `archve` was created

Practice

  1. In /root/lab/l14, rename `draft.txt` to `report.txt`.

  2. Move `a.jpg`, `b.jpg` and `c.jpg` from /root/lab/l14 into /root/lab/l14/photos with a single command.

  3. Move the whole directory /root/lab/l14/2023, with the notes inside it, into /root/lab/l14/archive.

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.