How to pack folder with tar XZ format with max compression ratio and saving all owner, permissions data
Assistant
To create a tar archive with the XZ compression format while preserving owner and permissions data and achieving the maximum compression ratio, you can use the tar command with specific options. The -c option is for creating an archive, -J specifies XZ compression, and -p preserves the permissions. The -v option is optional and is used for verbose output, so you can see which files are being processed.
Here's a command that accomplishes this:
tar -cvpJf archive-name.tar.xz --xz -C /path/to/source/folder .
Explanation:
-c: Create a new archive.
: Verbosely list files processed (optional).
-v
-p: Preserve permissions of files.
-J: Use XZ compression.
--xz: Ensures XZ compression is used with max level (note: as of later tar versions -J implies --xz, but it's good to be explicit for clarity).
-f archive-name.tar.xz: Specifies the name of the output archive file.
-C /path/to/source/folder: Change to the directory containing the folder you want to archive. This ensures only contents are compressed, not the full path.
.: Includes all contents of the specified directory.
For maximum compression, you can specify the compression level using --lzma2=dict=64MiB. To apply this option, you might need to modify it in the XZ_OPT environment variable. Here’s how to do it:
XZ_OPT="--lzma2=dict=64MiB" tar -cvpJf archive-name.tar.xz -C /path/to/source/folder .
Setting XZ_OPT with --lzma2=dict=64MiB indicates the maximum dictionary size, which generally provides the best compression but requires more memory. Adjust the dict parameter based on your available system memory for optimal results.