Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Files and Directories

Linux mv command

Reference for the Linux mv command: move and rename files and directories, avoid overwriting with -n, make backups, and know when a move is instant.

mv moves files and directories to a new place, and renames them. On Linux those are the same operation: giving a file a new path. When the source and destination are on the same filesystem, mv only changes the directory entries, so moving even a huge file is instant and keeps its inode, permissions and timestamps.

Across filesystems mv has to copy the data and then remove the original, which takes time and can fail halfway. Like cp, mv overwrites existing destination files silently unless you ask otherwise.

Terminal
mv [OPTION]... SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY

Try it

Bash Rename and move

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Protecting existing files

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
mv [-bfinuvTt] [--backup[=CONTROL]] SOURCE... DEST
Option Description
-n Do not overwrite an existing destination.
-i Ask before overwriting.
-f Never ask; overwrite. Cancels an earlier -i.
-u Move only when the source is newer than the destination or the destination does not exist.
-b, --backup[=CONTROL] Make a backup of each existing destination (name~).
-v Print what is done.
-t DIR Move all sources into DIR.
-T Treat the destination as a normal file, not as a directory.
-S SUFFIX Use SUFFIX instead of ~ for backups.

Returns: 0 on success, non-zero if any move failed.

Usage notes

  • Moving is not copying. Within one filesystem no data is copied, so a running program that has the file open keeps working. Across filesystems mv copies, then deletes, and preserves attributes where it can.
  • Directory targets: if DEST is an existing directory the source goes inside it; if it does not exist the source is renamed to it. mv dir newname therefore means two different things depending on whether newname exists. Use -T or -n to avoid surprises.
  • You cannot move a directory into itself (or into one of its own subdirectories).
  • Overwriting a non-empty directory with another directory fails ("Directory not empty").
  • Permissions: removing the old name needs write permission on the source directory, and creating the new one needs it on the destination directory. Permissions on the file itself do not matter (except when the sticky bit is set on the directory).
  • Batch renaming needs a loop or the rename tool; mv *.txt *.md does not work. In Bash, use for f in *.txt; do mv -- "$f" "${f%.txt}.md"; done.
  • Wildcard moves skip hidden files. mv dir/* other/ leaves .hidden behind; add dir/.[!.]* or set shopt -s dotglob.
  • Atomic replace: moving a file over an existing one on the same filesystem replaces it atomically. That is how safe config updates are done: write file.tmp, then mv file.tmp file.
  • Names starting with a dash need --.