Archive and Compress Files

Jan 28, 2023

#linux #archive


Archiving

tar - tape archive, a GNU archiving utility It also supports many kinds of compressions, including gzip, bzip2, lzip, lzma, lzop, xz, and gz.

Compression tools

Install the parallel version of compression packages bzip2 and gzip with your native package manager.

for gentoo user

# FILE: /etc/portage/env/app-arch/tar

# Use app-arch/lbzip2 for bzip2 and app-arch/pigz for gzip
EXTRA_ECONF="--with-bzip2=lbzip2 --with-gzip=pigz"

Then install relevant tools:

emerge --ask app-arch/lbzip2 app-arch/pigz
emerge --oneshot app-arch/tar

Extract tarball

tar -xvf <tarball>

Extract tarball into new directory --> first create folder mkdir <folder_to_extract>

tar -xvf <tarbal> -C <folder_to_extract>

Create tarball

tar -cf <tarball_name> <directory>

Archiving options

‘-z’	gzip
‘-j’	bzip2
‘-J’	xz

for gzip, bzip2 and xz

tar czf archive.tar.gz . # or folder_name
tar cjf archive.tar.bz2 .
tar cJf archive.tar.xz .

Compression algorithmm based on suffix --auto-compress (-a) flag

tar caf archive.tar.gz . # or folder_name

List the archives

tar tf archive.tar.gz

Compression

tar also provides highest compression tools available today called p7zip

--> 7za is wrapper for p7zip

To create an archive

7za a archive.7z archive

To extract archive

7za x -o<folder name> <archive name>

Preserving file attributes

To archive directory

tar cf - <directory> | 7za a -si <directory>.tar.7z

To extract a directory

7za x -so <directory>.tar.7z | tar xf -

into new directory

7za x -so 7z-compressed.tar.7z | tar xf - -C <folder_to_extract>