Compressing hundreds of 3D files without losing sleep: anatomy of a Bash script

If you work with 3D models, you know the drill: your disk fills up with .blend, .fbx, .obj, .max, .abc files, each weighing tens or hundreds of megabytes. Archiving everything by hand is tedious, doing it badly is dangerous. This script was born from a concrete need: batch-compress 3D files above a size threshold, verify that the archive is intact, and only then delete the original. No frills, no exotic dependencies: just Bash, find, and 7zz.

Let’s look at why it’s built this way and how it works, line by line, in the choices that actually matter.

The starting problem

Imagine you have a project folder with hundreds of 3D files. You want to reduce disk usage, but you can’t afford to lose a file because of a corrupted archive. The “ready-made” alternatives don’t convince:

  • Archive everything into a single archive: convenient, but if it gets corrupted you lose everything. And if you need to recover a single file, you have to extract the whole archive.
  • Compress by hand: slow, error-prone, and nobody verifies integrity before deleting.
  • Use a GUI tool: fine for a few files, not for hundreds scattered across subfolders.

The script solves exactly this: one archive per file, next to the original, verified before deletion.

The design choices that make the difference

1. One archive per file, not a single archive

Each model.fbx becomes model-fbx.7z in the same folder. Why?

  • Granularity: if one archive gets corrupted, you lose one file, not the entire collection.
  • Easy recovery: you know exactly where to find the archive for a given file.
  • Compatibility with the workflow: the project folder stays navigable, with file/archive pairs side by side.

The name includes the original extension (-fbx, -blend, etc.) to avoid collisions when files with the same base name but different extensions coexist in the same folder.

2. Minimum size threshold

Compressing a 2 KB file makes no sense: the archive could be larger than the original, and you waste time. The script uses find -size with a configurable threshold (50k by default). The value uses find syntax, so you can write 2M, 1G, or leave a bare number for 512-byte blocks.

The human_size function converts the threshold into a readable form (50KB) for the messages, so the user immediately understands what’s happening.

3. Verify before deleting

This is the heart of safety. After creating the archive, the script runs 7zz t (test) and only if the test passes does it delete the original. If verification fails:

  • the partial archive is removed (cleanup_partial_archive),
  • the original stays intact,
  • the failed_count counter increases.

The rule is simple: no file is deleted without a successful verification. This is why the script is usable on data you can’t afford to lose.

4. Compression with zstd, not lzma2

By default the script uses zstd at level 17 instead of the classic lzma2. Why?

  • Speed: zstd is much faster than lzma2 at comparable ratios, especially on large files.
  • Multithreading: zstd scales well across many cores, and the script passes -mmt=0 (all cores) by default.
  • Still excellent ratio: at level 17 zstd reaches ratios close to lzma2 on many data types.

You can still change method="lzma2" if you prefer the classic, but for 3D files (which often contain already-compressed geometry and textures) zstd is the pragmatic choice.

5. Handling interrupts and errors

The script is designed to be interruptible without leaving junk behind:

  • Ctrl+C / SIGTERM: the handle_interrupt trap removes the partial archive in progress and exits with code 130.
  • Unexpected errors: the handle_error trap performs the same cleanup and reports the exact line.
  • set -Eeuo pipefail: any failed command stops the script, preventing a silent error from leading to unwanted deletions.

The current_archive variable keeps track of the archive being written, so cleanup knows what to remove.

6. One failure doesn’t stop everything

compress_file never returns a non-zero code. If a file fails (archive already exists, verification failed, 7zz error), the failed_count counter increases and the script moves on to the next file. At the end, the summary distinguishes between “operation completed” and “operation completed with N errors”.

This is deliberate: in a batch of hundreds of files, a single problematic file must not interrupt work on the others.

7. Lightweight internationalization

The script detects the language from LC_ALL, LC_MESSAGES, or LANG (with the correct POSIX precedence) and supports Italian and English. The T function centralizes the messages, so adding a language only means adding a case.

It’s not “real” i18n (no .po files), but it’s enough for a personal script you want to make usable by others.

8. Support for “difficult” filenames

Filenames can contain spaces, newlines, strange characters. The script handles everything with:

  • find -print0 and read -r -d '' for NUL separation,
  • sort -z (if available) for sorting without breaking names,
  • -- before paths in all commands (rm, stat, 7zz) to prevent a name starting with - from being interpreted as an option.

It’s the kind of detail that seems excessive until you hit a file called -model.fbx and realize that without -- the script treats it as a flag.

9. Two usage modes

The script works in two ways:

  • With arguments: ./script.sh file1.fbx file2.blend compresses only the specified files.
  • Without arguments: recursively searches the current directory for all files with the configured extensions and above the threshold, shows them sorted, asks for confirmation, and proceeds.

The interactive mode is designed to be safe: it shows what it will do before doing it, and waits for ENTER. If something doesn’t convince you, Ctrl+C and nothing has happened.

What it doesn’t do (and that’s fine)

  • It’s not recursive on formats: if you add a new 3D format, you have to add it to extensions_config. There’s no auto-discovery.
  • It’s not parallel: it compresses one file at a time. 7zz uses all cores for the single file, but it doesn’t launch multiple compressions in parallel. For personal use it’s enough; for huge batches you’d need xargs -P.
  • It’s not a backup: it deletes the original after verification. If you want a backup, use another tool. The script assumes that the verified archive is the backup.
  • It doesn’t handle split archives: if you need 4 GB volumes for the cloud, you’ll have to add -v4g to the 7zz options. It’s a one-line change, but it’s not included.

How to use it

  1. Install 7zz (the 7-Zip binary for Linux). On Arch/CachyOS: pacman -S 7zip.
  2. Save the script as compress3d.sh and make it executable: chmod +x compress3d.sh.
  3. Edit the variables at the top: method, level, min_size, extensions_config.
  4. Run it in the project folder: ./compress3d.sh (interactive mode) or ./compress3d.sh model.fbx (single files).

In summary

This script isn’t “smart”: it doesn’t use AI, it doesn’t do magic. It’s prudent. It verifies before deleting, it doesn’t stop at the first error, it cleans up partials, it handles difficult names, and it shows you what it’s about to do before doing it. These are the qualities that matter when you work on data you can’t afford to lose.

If you use it, read it first. If you modify it, keep the golden rule: no deletion without verification. Everything else is negotiable.

Leave a Reply