mv — Move and Rename Files

Move or rename files and directories. Moves files between locations or renames them in place. Within the same filesystem, mv is instant regardless of file size.

mv moves files and directories to a new location or renames them – and within the same filesystem it is instant, because only the directory entry changes and no data is copied. That speed comes with a catch: without -i or -n, mv overwrites an existing destination file silently and without asking – the old contents are gone for good. This guide walks you through everyday moving and renaming, how to control overwriting, create backups, and rename whole groups of files with glob patterns or shell loops.

Basic Moving & Renaming

mv <source> <destination> — Move a file to a new location or rename it.

mv report.txt /archive/report.txt

mv <old_name> <new_name> — Rename a file in the same directory.

mv draft.md final.md

mv <file1> <file2> <directory> — Move multiple files into a directory.

mv index.html style.css app.js /var/www/html/

mv *.txt <directory> — Move all files matching a glob pattern into a directory.

mv *.log /var/log/archive/

mv <source_dir> <destination_dir> — Move or rename an entire directory.

mv ./old-project/ ./new-project/

Overwrite Control

mv -i <source> <destination> — Interactive mode. Prompt before overwriting an existing file.

mv -i new-config.yml /etc/app/config.yml

mv -n <source> <destination> — No clobber. Never overwrite an existing file.

mv -n defaults.conf /etc/app/config.conf

mv -f <source> <destination> — Force. Overwrite without prompting (default behavior).

mv -f updated.bin /usr/local/bin/app

mv -u <source> <destination> — Update. Only move when source is newer than destination or destination is missing.

mv -u *.html /var/www/html/

mv --update=none <source> <destination> — Explicit update mode: none (same as -n), all (default), or older (same as -u).

mv --update=older ./assets/* /var/www/assets/

Backup Options

mv -b <source> <destination> — Create a backup of the destination file (appends ~ suffix) before overwriting.

mv -b new.conf /etc/app/app.conf

mv --backup=numbered <source> <destination> — Create numbered backups (.~1~, .~2~, etc.) of the overwritten file.

mv --backup=numbered config.yml /etc/app/config.yml

mv --backup=existing <source> <destination> — Use numbered backups if any already exist, otherwise use simple ~ backup.

mv --backup=existing data.json /var/data/data.json

mv -S '<suffix>' <source> <destination> — Use a custom suffix for backup files instead of the default ~.

mv -b -S '.bak' config.yml /etc/app/config.yml

Verbosity & Target

mv -v <source> <destination> — Verbose mode. Print each file as it is moved.

mv -v *.log /archive/logs/

mv -v <old_name> <new_name> — Rename with confirmation output showing old and new name.

mv -v draft-v1.md release-notes.md

mv -t <directory> <file1> <file2> — Specify the target directory first. Useful with xargs and find.

find . -name '*.bak' -print0 | xargs -0 mv -t /tmp/backups/

mv -T <source> <destination> — Treat destination as a normal file, not a directory. Fails if destination is a directory.

mv -T old-dir/ new-dir/

Bulk Renaming Patterns

for f in *.txt; do mv "$f" "${f%.txt}.md"; done — Rename all .txt files to .md by changing the extension.

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

for f in *; do mv "$f" "$(echo $f | tr 'A-Z' 'a-z')"; done — Rename all files in current directory to lowercase.

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

for f in *\ *; do mv "$f" "${f// /_}"; done — Replace spaces with underscores in all filenames.

for f in *\ *; do mv "$f" "${f// /_}"; done

for f in *.jpg; do mv "$f" "photo_$(printf '%03d' $i).jpg"; ((i++)); done — Sequentially number all JPG files with zero-padded names.

i=1; for f in *.jpg; do mv "$f" "photo_$(printf '%03d' $i).jpg"; ((i++)); done

rename 's/<pattern>/<replacement>/' <files> — Perl rename tool. Bulk rename files using regex (not available on all systems).

rename 's/\.jpeg$/.jpg/' *.jpeg

rename 's/^/prefix_/' <files> — Add a prefix to all matching filenames using Perl rename.

rename 's/^/2024_/' *.log

rename 'y/A-Z/a-z/' <files> — Transliterate filenames from uppercase to lowercase using Perl rename.

rename 'y/A-Z/a-z/' *.TXT

Common Patterns

mv <file> <file>.bak — Create a quick backup by appending .bak to the filename.

mv config.yml config.yml.bak

mv <file>.bak <file> — Restore a file from a .bak backup.

mv config.yml.bak config.yml

mv <file> /tmp/ — Move a file to /tmp instead of deleting it (safer than rm).

mv unwanted-file.log /tmp/

mv <dir>/{old_name,new_name} — Rename a file using brace expansion (shell shorthand).

mv /etc/app/{config.yml.old,config.yml}

find <dir> -name '<pattern>' -exec mv {} <dest>/ \; — Find files by pattern and move them to a destination directory.

find ./downloads/ -name '*.pdf' -exec mv {} ./documents/ \;

find <dir> -name '<pattern>' -print0 | xargs -0 mv -t <dest>/ — Find and move files safely (handles filenames with spaces).

find . -name '*.tmp' -print0 | xargs -0 mv -t /tmp/cleanup/

find <dir> -maxdepth 1 -mtime +<days> -exec mv {} <archive>/ \; — Move files older than N days to an archive directory.

find ./logs/ -maxdepth 1 -mtime +30 -exec mv {} ./archive/logs/ \;

Conclusion

mv is among the most-used commands there is: one tool for both moving and renaming, with no noticeable wait within the same filesystem because no data is copied. The biggest pitfall is silent overwriting – for important destinations, get into the habit of -i (prompt) or -n (never overwrite), or have -b create a backup automatically. To rename many files at once, shell loops and the Perl rename tool are your friends; across filesystem boundaries, though, mv actually copies the data and is no longer instant.

Further Reading

  • cp – copy files and directories
  • rm – remove files and directories
  • rename – batch-rename files with patterns