Data compression is the process of removing redundancy from data
Compression Algorithms
Lossless
preserves all the data contained in the original
Lossy
removes data as the compression is performed to allow more compression to be applied
example: JPEG, MP3
All tools discussed below use lossless compression
gzip
used to compress data
it replaces the original file with a compressed version of the original
It can only compress single file at a time
gunzip is used to uncompress data
zcat and zless can be used to view the compressed data
# compress to sample.txt.gzgzip sample.txt# test integrity with verbose flaggzip -tv foo.txt.gz# view contents of sample.txt on stdoutzcat sample.txt.gz# view contents of sample.txt using lesszless sample.txt.gz# uncompress to sample.txtgunzip sample.txt.gz
bzip2
same as gzip but with different algorithm
better compression than gzip but slower
# compress to sample.txt.bz2bzip2 sample.txt# test integrity with verbose flagbzip2 -tv foo.txt.bz2# view contents of sample.txt on stdoutbzcat sample.txt.bz2# view contents of sample.txt using lessbzless sample.txt.bz2# uncompress to sample.txtbunzip2 sample.txt.bz2
Archiving
Archiving is the process of gathering up many files and bundling them together into a single large file
It is often done as part of system backups
tar
Tape Archive
used for archiving files
Unless we are operating as the superuser, files and directories extracted from archives take on the ownership of the user performing the restoration, rather than the original owner
# archive foldertar cf test.tar <folder># list contents with verbose flagtar tvf test.tar# extract archive on new foldercd /path/to/new/foldertar xf /path/to/test.tar# extract single filetar xf /path/to/test.tar /path/to/extract
Archiving with Compression
.tgz or .tar.gz extensions are used for gzip compression
.tbz or .tar.bz2 extensions are used for bzip2 compression
# archive and compress with gziptar czf test.tgz <folder># archive and compress with bzip2tar cjf test.tbz <folder># list contents with verbose flagtar tvf test.tgz# extract and uncompress gziptar xzf test.tgz# extract and uncompress bzip2tar xjf test.tbz
zip
used for archiving and compression
originally designed for Windows, and less preferred in Linux world
cannot preserve file permissions and attributes cleanly in Linux
# compress a folderzip -r test.zip <folder># list contents with verbose flagunzip -lv test.zip# uncompressunzip test.zip