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 -print0 | tar --null -T – archives files with strange names safely

Archive
~/tips/tip-tar-null-files-from-find

find logs -name '*.log' -print0 | tar --null -T - -czf logs.tgz

Feeds a find result to tar by file name, so spaces and even newlines in names do not matter.

Backing up only some files means a list of names, and the usual way to build one is find. Passing that list to tar is where file names with odd characters start to bite.

find logs -name '*.log' -print0 | tar --null -T - -czf logs.tgz

find -print0 ends each name with a NUL byte instead of a newline. -T - tells tar to read the names to archive from standard input, and --null says those names are NUL terminated. The help text adds that --null implies --verbatim-files-from, so the names are taken literally.

The test folder had a file called app one.log and one with a newline in the middle of its name. The archive listed all of them, and cat -A shows the newline as \n:

logs/odd\nname.log$
logs/app2.log$
logs/app one.log$

With a plain newline separated list, find logs -name '*.log' | tar -T - -cf bad.tar, the name with spaces still worked. Only the one with the newline broke:

tar: logs/odd: Cannot stat: No such file or directory
tar: name.log: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors

The odd name is rare, and I’d still use the NUL form out of habit, since it costs nothing.

A caveat about a neighbour option: --remove-files deletes each file after it is added to the archive. Combined with this pipeline it turns a backup into a move. In the test folder it left only keep.txt, the file the pattern did not match. Run the command once without it, and list the archive with tar -tzf, before you let it delete anything.

Further reading: GNU tar manual

More Archive tips tar -xzf archive.tgz --one-top-level tar -cf ../recent.tar --newer-mtime='1 day ago' . tar -df backup.tar tar --sort=name --mtime=2026-01-01T00:00:00Z \ tar --exclude-vcs -czf project.tar.gz project/ All Archive tips (7) All topics

Link