brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: find ! -exec gzip -t lists the archives that are broken

Find
~/tips/tip-find-exec-as-test-gzip

find /backup -name '*.gz' ! -exec gzip -t {} \; -print 2>/dev/null

Uses a command's exit status inside find to keep only the files that fail a check.

-exec isn’t only an action. It’s also a test: it’s true when the command exits with 0 and false otherwise. That turns any checking tool into a find filter.

find /backup -name '*.gz' ! -exec gzip -t {} \; -print 2>/dev/null

gzip -t tests an archive without unpacking it. For a good file it exits with 0, for a damaged one with an error. The ! flips that, so -print only runs for files that failed. A real archive stays quiet, and a text file renamed to .gz is listed. 2>/dev/null hides gzip’s own error messages, so only the names come out.

The pattern works with other tools that exit with an error on bad input: bzip2 -t, xz -t, unzip -tq, or python3 -m json.tool for JSON files.

Note the \; instead of +. With + find runs one command for many files, and the exit status then says nothing about a single file, so the test form needs one run per file. That’s slower on thousands of files, but for a check of a backup folder I think it’s fine. A passing test only means the archive is readable, by the way, not that it holds the files you expect.

Further reading: find(1) manual page, gzip manual

More Find tips find / -maxdepth 1 -fstype proc find /srv/downloads -type f -size +100M -size -1G -exec ls -lh {} + find /srv/data -depth -print find /srv -xdev -samefile /srv/downloads/movie.mkv find / -xdev -user olduser -ls 2>/dev/null All Find tips (46) All topics

Link