Packing with tar gzip, bzip2 and zip

Looking back trough my block i found my post about extracting archives under Linux. Now it's time to continue here with the How to put files into archives.

"tar.gz" with tar

Here are some common way to create your archives.

#Creates simple targetfile.tar without compression
tar cvf targetfile.tar sourcedir/*

#Zip everything beneath sourcedir to targetfile.tar.gz
tar cvzf targetfile.tar.gz sourcedir/*

#Bzip2 everything beneath sourcedir to targetfile.tar.bz2
tar cvjf targetfile.tar.bz2 sourcedir/*  

Parameters explanation:

  • c  or --create create a new archive
  • v or --verbose  verbosely list files processed
  • z or --gzip  usage of gzip compression (or also decompression, context dependent)
  • j or --bzip2 usage of bzip2 compression
  • f or --file  use archive file

Alternative with pipe usage:

tar -cf - sourcedir | gzip -c > filename.tar.gz  

gzip

Gzip is really standard one.

gzip test.txt  
# Test reuslt
ls -la  
-rw-r--r--  1 user user    29 Dec  8 16:52 test.txt.gz

also removes test.txt thisis handy in most cases.
Excerpt of useful options:

  • -c Write output on standard output;
  • -f Force compression or decompression even if the file has multiple links or the corresponding file already exists, or if the compressed data is read from or written to a terminal.
  • -l, -v List detail to files and compressions

bzip2

bzip2 compresses files using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding. Compression is generally considerably better than that achieved by more conventional compressors like gzip, and approaches the performance of the PPM family of statistical compressors.

The command-line options are deliberately very similar to those of GNU gzip, but they are not identical.

bzip2 test.txt  
# Test result
ls -la  
-rw-r--r--  1 user user    29 Dec  8 16:52 test.txt.bz2

zip

Some examples

#Zip every file in current directroy to file.zip.
#But hidden files like (.htaccess) are not included.
zip file.zip sourcedir/*

#also includes hidden files.
zip file.zip sourcedir/*.*  

The above examples include directories but still not their content recursively, -r option is required.

#Adds all files and directories recursivly.
zip file.zip -r /sourcedir/*

#Same as abowe with addtional enryption and password lock.
#Password is prompted on the terminal.
zip file.zip -re /sourcedir/*

#Splitts creted archive to parts not bigger than 2 Gigabytes.
zip -s 2g -r test.zip ./*  

Hope that helps someone.

Happy new year!!!