Compression

  • 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.gz
gzip sample.txt
 
# test integrity with verbose flag
gzip -tv foo.txt.gz
 
# view contents of sample.txt on stdout
zcat sample.txt.gz
 
# view contents of sample.txt using less
zless sample.txt.gz
 
# uncompress to sample.txt
gunzip sample.txt.gz

bzip2

  • same as gzip but with different algorithm
  • better compression than gzip but slower
# compress to sample.txt.bz2
bzip2 sample.txt
 
# test integrity with verbose flag
bzip2 -tv foo.txt.bz2
 
# view contents of sample.txt on stdout
bzcat sample.txt.bz2
 
# view contents of sample.txt using less
bzless sample.txt.bz2
 
# uncompress to sample.txt
bunzip2 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 folder
tar cf test.tar <folder>
 
# list contents with verbose flag
tar tvf test.tar
 
# extract archive on new folder
cd /path/to/new/folder
tar xf /path/to/test.tar
 
# extract single file
tar 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 gzip
tar czf test.tgz <folder>
 
# archive and compress with bzip2
tar cjf test.tbz <folder>
 
# list contents with verbose flag
tar tvf test.tgz
 
# extract and uncompress gzip
tar xzf test.tgz
 
# extract and uncompress bzip2
tar 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 folder
zip -r test.zip <folder>
 
# list contents with verbose flag
unzip -lv test.zip
 
# uncompress
unzip test.zip