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

“tar.gz” with tar

Here is 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 a standard one.

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

also removes test.txt this is 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

Let’s do it here by examples again:

#Zip every file in the current directory 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 recursively.
zip file.zip -r /sourcedir/*

#Same as above with additional encryption and password lock.
#Password is prompted on the terminal.
zip file.zip -re /sourcedir/*

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

Hope that helps someone. Happy new year!!!