← Full reference

mv

Move and rename files and directories

mv renames a file or moves it to another directory. Linux has no separate "rename" command — that's mv too. Directories are moved without -r. The sandbox has GNU coreutils 9.4.

Syntax

mv [options] old_name new_name       # rename
mv [options] source… dir/            # move into a directory
mv [options] -t dir source…          # same, directory first

If the last argument is an existing directory, the sources go inside it. Otherwise the source gets the new name (and an existing file with that name is replaced).

Options

OptionWhat it does
-iask before overwriting an existing file
-nnever overwrite; in 9.4 prints not replacing 'file' and returns 1
-fdon't ask. Of -f, -i, -n, the last one given wins
-umove only if the source is newer than the destination or the destination doesn't exist (--update=older)
--update=nonesame as -n
-vprint each action: renamed 'x' -> 'y'
-t dirdestination directory up front, the remaining arguments are sources (handy with xargs)
-Ttreat the destination as a plain name: mv -T a b replaces an empty directory b with a instead of putting a inside
-bkeep the replaced file as file~; --backup=numbered — file.~1~, file.~2~…
-S suffixbackup suffix instead of ~
--no-copydon't copy if a plain rename is impossible (another FS) — fail right away
--strip-trailing-slashesstrip trailing / from sources

Rename vs move across file systems

Within one file systemTo another file system
What happensonly the directory entry changes (the rename system call)data is copied + the original is deleted
Speedinstant, even for gigabytessame as cp
inodepreservednew
Atomicityyes: other processes see either the old or the new nameno: an interrupted move leaves a partial copy
-v printsrenamed 'a' -> 'b'copied 'a' -> 'b' and removed 'a'

df -T path or stat -c %m path (the mount point) shows where the FS boundary is.

Examples

# rename a file
mv report.txt report-2024.txt

# move several files into a directory
mv *.log /var/log/archive/

# rename a directory (no -r)
mv old_project new_project

# don't clobber existing files
mv -n *.conf /etc/myapp/

# ask before overwriting
mv -i draft.txt final.txt

# from find: destination directory via -t
find . -name '*.tmp' -print0 | xargs -0 mv -t /tmp/trash/

# atomic file replacement: write a temp file next to it, then rename
printf 'key=value\n' > /etc/myapp/app.conf.new && mv /etc/myapp/app.conf.new /etc/myapp/app.conf

# bulk rename: .txt → .md
for f in *.txt; do mv -- "$f" "${f%.txt}.md"; done

Pitfalls

  • Silent overwrite. Without -i/-n an existing file with the same name is replaced without asking.
  • A typo in the directory name. mv file /srv/backpu (no such directory) is not an error — the file simply gets renamed to backpu. Add a trailing / (/srv/backup/) or use -t: then a nonexistent directory gives an error.
  • A directory into a non-empty directory with the same name doesn't merge: "cannot overwrite 'g/f2': Directory not empty". To merge — cp -a src/. dst/ && rm -r src or rsync --remove-source-files.
  • A move to another FS is not atomic and slow for big directories; if interrupted, half ends up at the destination and everything stays at the source.
  • Atomic replacement only works within one FS. Create the temp file in the same directory as the target, not in /tmp.
  • Names starting with - are taken as options: write mv -- -file new or mv ./-file new.

See also

cp · rm · rsync · ln · find · xargs · stat