The problem exists only with compressed archives. I suggest first creating and using an uncompressed archive and compressing it later when the cycle has passed.
tar cf my_app.tar --files-from /dev/null for app_item in "${app_items[@]}" do tar rf my_app.tar "$app_item" done gzip my_app.tar
As @Matthias noted in a comment, creating an empty archive (what we need in the first line) is usually rejected by tar . To get this done, we need a trick that differs from the operating system to the operating system:
- BSD:
tar cf empty.tar --from-file /dev/null - GNU (Linux):
tar cvf empty.tar --files-from /dev/null - Solaris:
tar cvf empty.tar -I /dev/null
This is explained in more detail on the page with which it is associated .
A completely different approach, which also works well with file names with spaces in them (but not with file names with new characters in them, so itβs not quite as general):
tar cfz my_app.tgz --files-from <(printf "%s\n" "${app_items[@]}")
And another way to do this (which also works with new characters and other files in file names:
eval tar cfz my_app.tgz $(printf "%q " "${app_items[@]}")
But, as usual, with eval you should know what you pass it to it and that none of this comes from an untrusted source, otherwise it can cause a security problem, so I recommend that you avoid this at all.
source share