# Copying, Diffing, and Patching MBTiles

## `mbtiles copy`

Copy command copies an mbtiles file, optionally filtering its content by zoom levels.

```bash
mbtiles copy src_file.mbtiles dst_file.mbtiles \
        --min-zoom 0 --max-zoom 10
```

This command can also be used to generate files of different [supported schema](<https://maplibre.org/martin/mbtiles-schema/index.md>).

```bash
mbtiles copy normalized.mbtiles dst.mbtiles \
         --dst-type flat-with-hash
```

The destination hashes its tiles with the source's [hash algorithm](<https://maplibre.org/martin/mbtiles-validation/#hash-algorithm>), or with the one `--hash-algorithm` names.

```bash
mbtiles copy src.mbtiles dst.mbtiles \
         --dst-type flat-with-hash --hash-algorithm xxh3
```

### Merging several files

`copy` takes any number of source files before the destination and copies them into it one after another. Two sources can hold the same tile, so `--on-duplicate` is required to say which one wins. `--diff-with-file` and `--apply-patch` take exactly one source.

```bash
mbtiles copy north.mbtiles south.mbtiles merged.mbtiles --on-duplicate override
```

## `mbtiles copy --diff-with-file`

This option is identical to using [`mbtiles diff ...`](<https://maplibre.org/martin/mbtiles-diff/index.md>). The following commands two are equivalent:

```bash
mbtiles diff file1.mbtiles file2.mbtiles diff.mbtiles

mbtiles copy file1.mbtiles diff.mbtiles \
        --diff-with-file file2.mbtiles
```

## `mbtiles copy --apply-patch`

Copy a source file to destination while also applying the diff file generated by `copy --diff-with-file` command above to the destination mbtiles file. This allows safer application of the diff file, as the source file is not modified.

```bash
mbtiles copy src_file.mbtiles dst_file.mbtiles \
        --apply-patch diff.mbtiles
```
