Automating backup or deployment processes with the zip command can silently skip missing files, resulting in incomplete archives. By default, zip issues a warning but continues if files are missing, which may go unnoticed in scripts or batch jobs. Configuring zip to treat any missing file as an error ensures that missing files halt the process, alerting you to problems immediately and preventing data loss.

Use the -MM Option to Stop on Missing Files

Step 1: Add the -MM flag to your zip command. This option instructs zip to treat missing files as a fatal error and stop the process. For example, if you want to archive file1.txt and file2.txt into archive.zip:

zip -MM archive.zip file1.txt file2.txt

If either file1.txt or file2.txt does not exist, zip will display an error and exit without creating or modifying the archive. This prevents the creation of incomplete archives and helps you quickly identify missing files in automated workflows.

Step 2: Check the exit code of the zip command in scripts. When -MM is used and a file is missing, zip will exit with a nonzero status. You can use this exit code to halt scripts or trigger alerts. For example, in a shell script:

zip -MM archive.zip file1.txt file2.txt
if [ $? -ne 0 ]; then
  echo "Error: One or more files are missing. Archive not created."
  exit 1
fi

This practice ensures failures are immediately visible and can be handled appropriately, reducing the risk of propagating errors downstream.


Alternative: Manually Verify Files Before Archiving

If you are using an older version of zip that does not support the -MM option, you can manually check for file existence before running the zip command.

Step 1: Use a shell loop or command to verify that all files exist. For example, in a bash script:

for file in file1.txt file2.txt; do
  if [ ! -e "$file" ]; then
    echo "Error: $file is missing."
    exit 1
  fi
done
zip archive.zip file1.txt file2.txt

This approach stops the process before zip runs if any file is missing, ensuring you only create complete archives. While this method requires more scripting, it works reliably on systems where -MM is unavailable.


Considerations for Scripting and Automation

When integrating zip into automated workflows, always validate that your method reliably stops on missing files. Relying on warnings alone can result in silent data loss, especially in unattended or scheduled jobs. Using -MM or pre-checking files before archiving improves reliability and helps maintain data integrity.

If you are unsure whether your version of zip supports -MM, check the manual page with man zip or zip --help.


Using the -MM option or pre-checking files ensures your archives are always complete and reliable. Taking these steps saves time and prevents headaches from missing data down the line.