Archiving & compression

    Contents

  1. The .tar file
  2. Zip files(.zip)
  3. Passphrase encrypted archives

Using archive files you can combine multiple files together and compress them to easily share them between computers.

The .tar file #

Archiving in Linux is often done with the .tar file format and the tar program.

The -v (verbose) flag lists the files being compressed/extracted.

The -c/--create is used to create .tar archives. The -f option specifies the output file:

tar -cf archive.tar file1 file2 file3

Use -x/--extract option to extract files of a tar archive, you can optionally specify a directory with -C or list the files being exracted using -v:

tar -xf archive.tar
tar -xf archive.tar -C /directory
tar -xvf archive.tar.gz

Use the -t flag to list files inside an .tar file:

tar -tf archive.tar

Compression formats #

The tar program supports many compression formats. During extracting it will automatically detect the compression format.. Use the following flags while creating an archive to compress:

  • -z for gzip (.tar.gz)
  • -J for xz (.tar.xz)  
  • --zstd for zstd/zstandard (.tar.zst) a relatively new compression format that offers very good compression ratios.

Example (creates archive with gzip compression):

tar -czf archive.tar.gz file1 file2 file3

tar --zstd -cf archive.tar.zst file1 file2 file3
# Ultra compression (slow)
tar -I 'zstd --ultra -22' -cf archive.tar.zst file1 file2 file3
tar -I 'zstd --ultra 22 --threads=4 --long' -cf archive.tar.zst file1 file2 file3
tar -I zstd -xvf archive.tar.zst

Zip files(.zip) #

Zip is another archiving format often used on Windows. To create and extract zip files you need the zip and unzip packages respectively.

Use the zip package.

zip archive.zip file1 file2

-r for recursively directories

unzip to unzip it

Passphrase encrypted archives #

You can pipe the tar command to gpg to create password encrypted archives:

# Compress & encrypt
tar --zstd -cvf - mydir | gpg -c > archive.tar.zst.gpg
# Extract & decrypt
gpg -d archive.tar.zst.gpg | tar -I zstd -xvf -