Terminal GuideTerminal Guide

mv Command Guide

The mv command moves and renames files and directories. Learn how to relocate and rename files safely.

5 min readLast updated: 2024
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Basic

mv file.txt dir/Move file to directory
mv old.txt new.txtRename file
mv dir1/ dir2/Move/rename directory

Options

-iPrompt before overwrite
-nDo not overwrite
-vVerbose output

Safety

-uMove only if newer
-bCreate backup
--backup=numberedNumbered backups

Common

mv *.txt dir/Move multiple files
mv -t dir/ f1 f2Target directory first
mv file{,.bak}Quick backup

Downloadable Image Preview

Failed to generate preview

Basic Usage

mv is used for both moving and renaming files.

bash
# Rename a file
mv oldname.txt newname.txt

# Move file to directory
mv file.txt /path/to/directory/

# Move and rename
mv file.txt /path/to/directory/newname.txt

# Move multiple files to directory
mv file1.txt file2.txt /path/to/directory/

Common Options

mv Options

-iPrompt before overwriting
-nDo not overwrite existing files
-fForce overwrite without prompting
-uMove only when source is newer
-vVerbose output
-bCreate backup of destination files
-t dirMove all sources to directory

Renaming Files

bash
# Simple rename
mv report.txt final_report.txt

# Rename with different extension
mv script.txt script.sh

# Rename directory
mv old_project/ new_project/

Moving Files and Directories

bash
# Move file to directory
mv document.pdf ~/Documents/

# Move multiple files
mv *.txt /destination/

# Move directory
mv project_dir/ /new/location/
Info
Unlike cp, mv doesn't need -r for directories. Directories are moved automatically.

Preventing Overwrites

bash
# Ask before overwriting
mv -i file.txt /destination/

# Never overwrite
mv -n file.txt /destination/

# Only move if source is newer
mv -u file.txt /destination/
Warning
By default, mv will overwrite existing files without warning. Use -i in interactive sessions or -n in scripts.

Creating Backups

bash
# Create backup before overwriting
mv -b file.txt /destination/
# Creates file.txt~ as backup

# Specify backup suffix
mv --suffix=.bak -b file.txt /destination/
# Creates file.txt.bak

Batch Renaming

Using a for loop

bash
# Add prefix to all .txt files
for f in *.txt; do mv "$f" "prefix_$f"; done

# Change extension
for f in *.txt; do mv "$f" "${f%.txt}.md"; done

# Lowercase filenames
for f in *; do mv "$f" "$(echo $f | tr 'A-Z' 'a-z')"; done

Using rename command

bash
# Rename using regex (Perl rename)
rename 's/.txt$/.md/' *.txt

# Add prefix
rename 's/^/prefix_/' *.txt

# Remove spaces
rename 's/ /_/g' *

Practical Examples

Organize files by extension

bash
mv *.jpg images/
mv *.pdf documents/
mv *.mp3 music/

Move files older than 30 days

bash
find . -mtime +30 -exec mv {} /archive/ \;

Rename with timestamp

bash
mv file.txt "file_$(date +%Y%m%d_%H%M%S).txt"

Move all files to parent directory

bash
mv subdir/* .

Interactive move with verbose

bash
mv -iv *.txt /destination/

mv vs cp + rm

On the same filesystem, mv is instant (just renames the inode). Across filesystems, mv copies then deletes.

bash
# Same filesystem: instant
mv /home/user/file.txt /home/user/subdir/

# Different filesystem: copy + delete
mv /home/user/file.txt /mnt/external/

Summary

mv is essential for file organization. Key takeaways:

  • Use mv old new to rename
  • Use mv file dir/ to move
  • Use -i to prevent accidental overwrites
  • Use -b to create backups
  • Use rename or loops for batch operations

Related Articles