mv Command

The mv command moves files and directories from one location to another. It can also rename files and directories. Unlike cp, mv removes the original file after moving it.

Syntax

mv [OPTION]... SOURCE... DESTINATION

Description

The mv command performs two main functions: moving files/directories to different locations and renaming files/directories. It's essentially a cut and paste operation.

Key behaviors:

  • Moves files and directories
  • Renames files and directories
  • Removes original after moving
  • Can move multiple files to a directory
  • Preserves file permissions and timestamps

Common Options

Option Description
-i Interactive mode (prompt before overwrite)
-v Verbose mode (show what's being moved)
-f Force move (don't prompt)
-n No overwrite (don't overwrite existing files)
-u Update (move only if source is newer)
-b Backup existing files before overwriting

Examples

Rename a file

mv oldname.txt newname.txt

Renames oldname.txt to newname.txt in the same directory

Move file to different directory

mv file.txt /home/user/Documents/

Moves file.txt to the Documents directory

Move and rename simultaneously

mv file.txt /home/user/Documents/newname.txt

Moves file.txt to Documents and renames it to newname.txt

Move multiple files

mv file1.txt file2.txt file3.txt /destination/

Moves multiple files to destination directory

Move directory

mv old_directory new_directory

Renames or moves entire directory

Interactive move (prompt before overwrite)

mv -i file.txt /destination/

Prompts before overwriting existing files

Verbose move

mv -v file.txt /destination/

Shows what files are being moved

Backup before overwrite

mv -b file.txt /destination/

Creates backup of existing file before overwriting

⚠️ Important Notes

  • mv removes the original file after moving (unlike cp)
  • Be careful when moving to existing filenames - they will be overwritten
  • Use -i flag to prompt before overwriting files
  • Moving across filesystems may be slower (copy + delete operation)
  • Cannot move directory into itself or its subdirectories

See also