# 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.

Source: https://www.jpkc.com/db/en/cheatsheets/files-text/mv/

<!-- PROSE:intro -->
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.
<!-- PROSE:intro:end -->

## Basic Moving & Renaming

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

```bash
mv report.txt /archive/report.txt
```

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

```bash
mv draft.md final.md
```

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

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

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

```bash
mv *.log /var/log/archive/
```

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

```bash
mv ./old-project/ ./new-project/
```

## Overwrite Control

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

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

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

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

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

```bash
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.

```bash
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).

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

## Backup Options

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

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

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

```bash
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.

```bash
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 ~.

```bash
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.

```bash
mv -v *.log /archive/logs/
```

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

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

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

```bash
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.

```bash
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.

```bash
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.

```bash
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.

```bash
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.

```bash
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).

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

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

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

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

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

## Common Patterns

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

```bash
mv config.yml config.yml.bak
```

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

```bash
mv config.yml.bak config.yml
```

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

```bash
mv unwanted-file.log /tmp/
```

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

```bash
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.

```bash
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).

```bash
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.

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

<!-- PROSE:outro -->
## 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

- [GNU coreutils: mv invocation](https://www.gnu.org/software/coreutils/manual/html_node/mv-invocation.html) – official reference for every option
- [Linux man page: mv(1)](https://man7.org/linux/man-pages/man1/mv.1.html) – the canonical manual page
<!-- PROSE:outro:end -->

## Related Commands

- [cp](https://www.jpkc.com/db/en/cheatsheets/files-text/cp/) – copy files and directories
- [rm](https://www.jpkc.com/db/en/cheatsheets/files-text/rm/) – remove files and directories
- [rename](https://www.jpkc.com/db/en/cheatsheets/files-text/rename/) – batch-rename files with patterns

