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
| Option | What it does |
|---|---|
-i | ask before overwriting an existing file |
-n | never overwrite; in 9.4 prints not replacing 'file' and returns 1 |
-f | don't ask. Of -f, -i, -n, the last one given wins |
-u | move only if the source is newer than the destination or the destination doesn't exist (--update=older) |
--update=none | same as -n |
-v | print each action: renamed 'x' -> 'y' |
-t dir | destination directory up front, the remaining arguments are sources (handy with xargs) |
-T | treat the destination as a plain name: mv -T a b replaces an empty directory b with a instead of putting a inside |
-b | keep the replaced file as file~; --backup=numbered — file.~1~, file.~2~… |
-S suffix | backup suffix instead of ~ |
--no-copy | don't copy if a plain rename is impossible (another FS) — fail right away |
--strip-trailing-slashes | strip trailing / from sources |
Rename vs move across file systems
| Within one file system | To another file system | |
|---|---|---|
| What happens | only the directory entry changes (the rename system call) | data is copied + the original is deleted |
| Speed | instant, even for gigabytes | same as cp |
| inode | preserved | new |
| Atomicity | yes: other processes see either the old or the new name | no: an interrupted move leaves a partial copy |
-v prints | renamed '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/-nan 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 tobackpu. 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 srcor 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: writemv -- -file newormv ./-file new.