Sagar.BlogArticle
All posts
All posts
Linux

mv — Move and Rename Files in Linux

mv both moves files to new locations and renames them — it's the same command. Learn the difference, the safety flags, and everyday patterns.

January 13, 20254 min read
LinuxFilesCommands

mv does two things that feel different but use the same underlying operation — changing a file's path:

  • Rename — same directory, different name
  • Move — different directory, same or different name

There's no separate rename command for basic use cases in Linux. mv handles both.

Options

mv Options

OptionLong FormDescription
-i--interactivePrompt before overwriting
-f--forceForce overwrite without prompting
-n--no-clobberNever overwrite existing files
-u--updateMove only if source is newer than destination
-v--verboseShow each operation
-b--backupMake a backup of overwritten files

Examples

mv-examples.sh
# ─── Renaming ─────────────────────────────────────────────────
mv old_name.txt new_name.txt         # Rename file
mv old_folder/ new_folder/           # Rename directory
mv report.doc report_v2.doc          # Rename with versioning

# ─── Moving ──────────────────────────────────────────────────
mv file.txt Documents/               # Move to Documents/ (keeps name)
mv file.txt Documents/newname.txt    # Move and rename at the same time
mv file1.txt file2.txt mydir/        # Move multiple files to a directory
mv folder/ /var/www/                 # Move entire directory

# ─── Safety ──────────────────────────────────────────────────
mv -i source.txt dest.txt            # Ask before overwriting
mv -n source.txt dest.txt            # Never overwrite
mv -b source.txt dest.txt            # Backup: dest.txt → dest.txt~
mv -v file.txt destination/          # Show: renamed 'file.txt' -> 'destination/file.txt'

mv Overwrites Without Warning by Default

Unlike cp, mv will silently overwrite the destination if it already exists — no prompt, no backup. Always use mv -i when moving files to a location where a file with the same name might exist. Or use mv -b to auto-create backups (file.txt~).

Practical Patterns

# Organise downloads by type
mv ~/Downloads/*.pdf ~/Documents/PDFs/
mv ~/Downloads/*.jpg ~/Pictures/

# Add a date suffix to a folder before archiving
mv project/ project_2025-01/

# Rename all .txt files to .md (using a loop)
for f in *.txt; do mv "$f" "${f%.txt}.md"; done

# Atomically replace a file (mv is atomic on the same filesystem!)
cp -a config.yaml config.yaml.tmp
# edit config.yaml.tmp...
mv -f config.yaml.tmp config.yaml   # Instantly swaps in the new file

Quick Check

You run `mv report.txt /archive/report.txt` but `/archive/report.txt` already exists. What happens by default?

Exercise

Practice mv:

  1. Create a file draft.txt and rename it to final.txt
  2. Create a directory archive/ and move final.txt into it
  3. Now move and rename in one step: move archive/final.txt to the current directory as final_v1.txt
  4. Create two files a.txt and b.txt, then try mv -i a.txt b.txt — what does -i show you?