Add just files in current directory into .7z file without recursing directories

Viewed 340

I want to add/update just the files of the current directory into archive.7z without recursing into lower directories. This works, but it's slow due to initialization for each file:

find -maxdepth 1 -type f | while read f;do 7za a ~/archive.7z "$f" ; done

This also works, and is much faster, but unsure how bulletproof it is for problematic file names with spaces and other problematic chars:

find -maxdepth 1 -type f -print0 | xargs -r0 7za a ~/archive.7z --

Shouldn't there be a more elegant way? Like with -T - in the tar command. I'm currently using 7za version 9.20 on Ubuntu16. Variants such as these below failed, they started recursing into lower directories which I don't want:

7za a -r- ~/archive.7z .
7za a -r- ~/archive.7z ./
7za a -r- ~/archive.7z *
7za a -r- ~/archive.7z -- *.*
7za a -r- ~/archive.7z -- "*"
7za a -r0 ~/archive.7z -- *.*
1 Answers
find . -maxdepth 1 -type f -exec 7z a archive.7z {} +

This way you call one 7z process (contrary to the syntax -exec {} \;) and this is expected to be fast. In fact it will use up to the maximum allowed arguments before raising a second process.


Also, regarding your query about filenames. The while loop is not acceptable. find -print0 | xargs -0 is good and fast. In general, you have to zero separate the output of find and read it as zero separated input on the next processing, to preserve filenames.

Related