Update Files in Archive with 7-Zip Command Line

Updating existing files within an archive using the 7-Zip command line is achieved primarily through the update command (u). This command compares the timestamps of files on your disk with those inside an existing archive, replacing older archived files with newer versions and optionally appending new files. This guide details the essential syntax, practical examples, and advanced switches needed to efficiently modify existing archives using 7-Zip's command-line interface.

Basic Syntax

The basic syntax for updating an archive is:

7z u <archive_name> <file_names_or_paths>
  • 7z: Calls the 7-Zip executable (7za or 7zr can also be used depending on your standalone package).
  • u: The update command. Unlike the add command (a), u checks timestamps to determine what needs to be changed.
  • <archive_name>: The archive to be modified (e.g., backup.zip, data.7z).
  • <file_names_or_paths>: The files, wildcards, or directories you want to update.

Standard Update Example

To update an archive with modified files from the current directory:

7z u project.zip report.docx

If report.docx on disk has a newer timestamp than the version stored inside project.zip, 7-Zip will replace the archived file. If the file is not already present in the archive, 7-Zip will add it.

To update multiple files or an entire folder:

7z u backup.7z ./source/*

Updating Only Existing Files (Without Adding New Ones)

By default, the u command will add newly discovered files that do not exist in the archive. If you only want to overwrite files that are already present in the archive, use the -u switch with custom state actions.

The -uq0 parameter tells 7-Zip not to add files that exist on the disk but do not exist in the archive:

7z u archive.7z *.txt -uq0

Understanding the Update Switch (-u)

The -u switch provides fine-grained control over how 7-Zip handles files based on their existence and timestamps. The switch uses the format -u[<action_set>] with specific state and action pairs:

  • State variables:

    • p: File exists in archive, but not on disk.
    • q: File exists on disk, but not in archive.
    • r: File on disk is older than file in archive.
    • s: File on disk is newer than file in archive.
    • t: File on disk is identical to file in archive.
  • Action codes:

    • 0: Ignore file.
    • 1: Copy file from old archive to new archive.
    • 2: Overwrite / compress file from disk.
    • 3: Create new archive file.

Example: Strictly Overwrite Newer Files Only

To ensure you only update files that are strictly newer and ignore all others:

7z u data.zip ./src/* -up1 -uq0 -ur1 -us2 -ut1

In this command:

  • p1: Keeps existing archive files if they are missing locally.
  • q0: Ignores new files on disk.
  • r1: Keeps the archive version if the local file is older.
  • s2: Updates the file if the local file is newer.
  • t1: Keeps the archive version if files are identical.

Recursive Directory Updates

To update an archive and traverse all subdirectories recursively, append the -r switch:

7z u website_backup.7z C:\WebRoot\* -r

This recursively scans C:\WebRoot\ and updates matching modified files within all subfolders of the target archive.