Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

mv

Move files and folders, or rename them.

Niveau
RHCSA-examen
Groep
Bestanden en mappen
Root nodig
Nee
Pakket
coreutils

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

mv SOURCE TARGET moves a file or folder. If the target is a new name in the same folder, it is a rename. Inside one file system, mv only changes the name, so even huge files move in an instant. Between file systems it copies the data and then deletes the source.

A moved file keeps its permissions, owner and SELinux label. That is a classic problem when you move files into /var/www/html: run restorecon after.

Zo schrijf je het

mv [OPTION]... SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...

Opties 17

Meest gebruikt

-i, --interactive
Ask before overwriting a file.
-f, --force
Do not ask before overwriting.
-n, --no-clobber
Never overwrite an existing file.
-u
Move only when the source is newer than the target, or the target is missing.
--update[=<which>]
Control which existing files are replaced: all, none, none-fail, older.
-v, --verbose
Print each move.
-t, --target-directory=dir
Move all sources into this folder.
-T, --no-target-directory
Treat the target as a normal file, never as a folder.
--backup[=<control>]
Make a backup of each target that would be overwritten.
-b
Like --backup without an argument.
-S, --suffix=suffix
The ending of backup files (default ~).
--no-copy
Do not copy when renaming fails (when the target is on another file system).
--strip-trailing-slashes
Remove the slash at the end of each source.
-Z, --context
Give the moved file the default SELinux context of its new place.
--debug
Explain how each file is moved (implies -v).
--help
Show help.
--version
Show the version.

Voorbeelden 11

$ als gewone gebruiker# als root (of met sudo)

  1. 1Rename a file

    mv report.txt report-2026.txt

    Same folder, new name.

  2. 2Move files into a folder

    mv *.log /var/log/archive/

    All matching files go into the folder.

  3. 3Move and fix the SELinux label at once

    mv -Z /tmp/index.html /var/www/html/

    Without -Z the file keeps the user_tmp_t label and Apache shows "403 Forbidden".

  4. 4Ask before overwriting

    mv -i new.conf /etc/myapp/app.conf

    Safe when the target may exist.

  5. 5Do not overwrite existing files

    mv -n uploads/* /srv/files/

    Files that already exist in the target stay as they are.

  6. 6Rename a folder

    mv /srv/app-old /srv/app-2025

    Folders need no -r for mv.

  7. 7Replace a config file atomically

    cp app.conf /etc/myapp/app.conf.new && mv /etc/myapp/app.conf.new /etc/myapp/app.conf

    On the same file system the rename is atomic: a reader sees the old or the new file, never half of it.

  8. 8Switch a release with a symlink (zero downtime deploy)

    ln -sfn /srv/app/releases/42 /srv/app/current.tmp && mv -T /srv/app/current.tmp /srv/app/current

    mv -T replaces the link in one atomic step.

  9. 9Keep a backup of the file you overwrite

    mv --backup=numbered new.cfg app.cfg

    The old app.cfg becomes app.cfg.~1~.

  10. 10Move many files given by find

    find /tmp -maxdepth 1 -name "*.csv" -mtime +7 -exec mv -t /archive/ {} +

    -t makes it easy to use with find and xargs.

  11. 11Show each move

    mv -v *.jpg photos/

    Prints "renamed 'a.jpg' -> 'photos/a.jpg'".

Let op

  • mv overwrites without asking (normal users). Use -i or -n.
  • Moved files keep their old SELinux label. Use mv -Z or run restorecon -Rv on the new place.
  • A typo in the target can rename instead of move: mv file /backp creates a file named /backp.

Tips om te onthouden

  • Same folder = rename. Other folder = move. -Z = new place, new SELinux label.