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. Will include everything in the current dir "." tar cvf targetfile.tar ./* #Zip everything beneath current dir to targetfile.tar.gz tar cvzf targetfile.tar.gz ./* #Bzip2 everything beneath current dir to targetfile.tar.bz2 tar cvjf targetfile.tar.bz2 ./*
--createcreate a new archive
--verboseverbosely list files processed
--gzipusage of gzip compression (or also decompression, context dependent)
--bzip2usage of bzip2 compression
--fileuse archive file
There is an alternative if you need to split tar and zipping.
tar -cf - sourcedir | gzip -c > filename.tar.gz
Gzip is works really interesting. If executed with out arguments it would replace all files of the current fir with it’s gziped equivalents. Gzip compresses only single regular files one byone (no directories or symlinks) and creates a compressed file for each given file. By convention, the name of a file compressed with Gzip should end with either .gz or .z.
#gzip gzip test.txt # check ls -la -rw-r--r-- 1 user user 29 Dec 8 16:52 test.txt.gz
If you need to compress many files. You need to create a Tar archive and then compress the .tar file with Gzip. Gzip is most often used to compress text files, Tar archives, and web pages. If you looking for gzip decomplression tutorial, please consult my article on this topic
But how to keep the original file?
# Option 1 gzip -c mysorce-file.tar > filename.gz # Or use of -k param gzip -k mysorce-file.tar
How to gzip the whole tree
Add the -r (recursive) flag. Plese be warned! doing it on wrong dir might bring you in torubles!
gzip -r ./*
gzip and pipes
For pipe usage there is a handy -c flag
-cwrite output on standard output;
mysqldump database_name | gzip -c > database_name.sql.gz
Controlling gzip compression level
gzip allows you to specifycompression level. It reaches from 1 to 9.
--fast- fastest compression speed with minimal compression ratio
--best- slowest compression speed with maximum compression ratio.
The default compression level is
get maximum compression, you would run:
#Example of using maximum compression ratio gzip -9 myfile.txt
More useful options:
-fforce 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.
-vlist detail to files and compressions
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
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. To include the subelement the
-r option is required.
#Zips all files from the current and child directories recursively. zip file.zip -r ./* #Same as above with additional encryption and password lock. #Password is prompted on the terminal. zip file.zip -re ./* #Splits created archive to parts not bigger than 2 Gigabytes. zip -s 2g -r test.zip ./*
Hope that helps someone.