Files and directories
mv
Move or rename files and directories.
mv [OPTION...] SOURCE... DEST
mv does two jobs with one mechanism. Within one filesystem, moving is just renaming: the
directory entry changes, the data stays where it is, so even a huge directory moves instantly
and keeps its timestamps, owner and inode. Across filesystems, mv has to copy everything and
then delete the original, which takes time and can fail halfway.
mv old new renames. mv file... dir/ moves files into an existing directory. Directories need
no -r. Like cp, mv overwrites an existing destination without asking unless you add
-i (ask) or -n (never overwrite).
Renaming many files by a pattern is not something mv does itself; use a shell loop with
parameter expansion, shown below.
Examples
Rename a file
mv todo.txt tasks.txt
ls *.txt
file with spaces.txt notes.txt tasks.txt
todo.txt no longer exists; the same file is now called tasks.txt.
Move files into a directory
mkdir archive
mv logs/app.log.1 logs/error.log archive/
ls archive logs
archive: app.log.1 error.log logs: app.log
With a directory as the last argument, every source moves into it and keeps its name.
Rename a directory
mv photos images
ls -d images photos
ls: cannot access 'photos': No such file or directory images
Directories move like files, no -r needed. The ls confirms photos is gone and
images exists.
A rename keeps the same file
ls -i notes.txt
mv notes.txt meeting.txt
ls -i meeting.txt
9825349 notes.txt 9825349 meeting.txt
The inode number is unchanged: on the same filesystem, mv only rewrites the directory
entry. That is also why a process that has the file open keeps writing to it after a rename.
mv overwrites silently
mv todo.txt notes.txt
cat notes.txt
buy milk fix login bug write tests fix logout bug deploy v2.4
notes.txt already existed and has been replaced by the contents of todo.txt, with no
warning. The old notes are gone.
Refuse to overwrite with -n
mv -n todo.txt notes.txt
ls todo.txt notes.txt
notes.txt todo.txt
-n leaves both files alone because notes.txt exists.
Ask before overwriting
mv -i todo.txt notes.txt
head -1 notes.txt
mv: overwrite 'notes.txt'? Meeting notes, March
-i prompts when the target exists; the answer n (from standard input here) keeps the
original notes.txt.
See each move
mv -v data/*.csv .
renamed 'data/sales.csv' -> './sales.csv' renamed 'data/users.csv' -> './users.csv'
-v prints 'from' -> 'to' for each file, a useful record when a glob is involved.
Rename many files by pattern
Setup run before this example
touch report-1.txt report-2.txt report-3.txtfor f in report-*.txt; do
mv -- "$f" "${f%.txt}.md"
done
ls report-*
report-1.md report-2.md report-3.md
${f%.txt} strips the .txt suffix, and the loop renames each file. Quoting "$f" keeps
names with spaces intact, and -- protects against names starting with a dash.
Keep the file you are replacing
mv -b todo.txt notes.txt
ls notes.txt*
notes.txt notes.txt~
-b renames the existing notes.txt to notes.txt~ before the move replaces it.
Moving a file that does not exist
mv draft.txt final.txt
mv: cannot stat 'draft.txt': No such file or directory
mv reports the missing source and exits with 1.
A trailing slash catches typos in the destination
mv todo.txt archive/
mv: cannot move 'todo.txt' to 'archive/': Not a directory
archive does not exist. With the trailing slash, mv knows you meant a directory and
fails instead of renaming todo.txt to a file called archive.
Common pitfalls
mv a bwherebis an existing directory movesainto it; wherebdoes not exist, it renames. Usemv -T a bto always mean rename, or a trailing slash to always mean into.A cross-filesystem
mvof a big tree that gets interrupted leaves a partial copy and an intact original. For large moves between disks,rsync--remove-source-filescan resume.Moving a file within the same filesystem keeps its original permissions and owner; it does not pick up the new directory's defaults.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
-n,-i,-fand-vexist on both.-T,-t DIR,--backup/-band--exchangeare GNU options; check your system's man page on BSD/macOS.
Options worth knowing
A selection, described in our own words. For the full list, see mv(1) or mv --help.
| Option | What it does |
|---|---|
-i, --interactive | Ask before overwriting. |
-n, --no-clobber | Never overwrite an existing file. |
-f, --force | Do not prompt before overwriting. |
-v, --verbose | Print each move. |
-u | Move only when the source is newer or the destination is missing. |
-b | Back up each destination file that would be replaced. |
--backup[=CONTROL] | Back up replaced files: numbered, existing, simple or none. |
-S, --suffix SUFFIX | Use SUFFIX for backups instead of ~. |
-t, --target-directory DIR | Move all sources into DIR. |
-T, --no-target-directory | Treat DEST as a name, never a directory to move into. |
--exchange | Swap SOURCE and DEST atomically (needs filesystem support). |
Related
cpCopy files and directories, optionally keeping their timestamps, permissions and links.rmDelete files, and with -r whole directory trees. There is no undo.lnMake links between files, symbolic (-s) or hard.rsyncCopy and synchronize files and directories, locally or over ssh, transferring only what changed.- Parameter expansionThe ${...} operators for defaults, lengths, substrings, trimming prefixes and suffixes, search and replace, and case conversion, without calling sed or cut.
- Loopsfor over lists, files and ranges, C-style for, while and until, break and continue, retry loops, and redirecting a whole loop's input or output.