'Extract files and merge/append them from multiple archives

Any way to extract tar.gz archives appending instead overwriting extracted files?

Apache logs are split like that: Apr-2022.tar.gz Apr-2022.tar.gz.1 etc... Apr-2022.tar.gz.11

Each archive file contains domain.log and domain.error.log files.

How to achieve that after extraction there would be domain.log and domain.error.log with data chunks form each archive.

Native Unix tar and gunzip doesn't have option to append instead overwriting/skipping file.



Solution 1:[1]

Edit: This is gnu tar specific, not "native unix tar"

Assuming there is one file in the archive and the filename in each archive is the same then the following concats to the same file.

tar -xvzf Apr-2022.tar.gz --to-command='cat - >>$TAR_FILENAME'
tar -xvzf Apr-2022.tar.gz.1 --to-command='cat - >>$TAR_FILENAME'
...
...
tar -xvzf Apr-2022.tar.gz.11 --to-command='cat - >>$TAR_FILENAME'

The tar command sets several TAR_* environment variables before starting the command given in the --to-command option. In this case you can use TAR_FILENAME or TAR_REALNAME

Or you use your own filename, in which case it doesn't matter if the filenames in the archives are different.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1