gzip, bzip2, 7zip, and tar

Overview

File compression, management and backup are important skills to have no matter your level of expertise. Some of the older compression tools are compress, gzip, bzip2. 7zip is a newer tool that will compress folders as well as files and it has a higher compression ratio. Tar is an archive backup utility that will compress files also. Tar is useful and necessary if you want to compress a folder. Typically you would make a tar file first and then compress it with gzip or bzip2. A tar file that is compressed typically has a .tar.gz or .tgz file extension and are referred to as “tarballs”.

gzip – file compression utility, makes .gz compressed files
bzip2 – file compression utility, makes .bz2 compresses files
7zip – file and folder compression utility, makes .7z files
tar – archive as well as file and folder compression utility, makes .tar and .tar.gz and .tar.bz2 files

Exercise

This is a step-by-step tutorial that will walk you through some standard Linux commands and utilities. In this walk through we will cover gzip, bzip2, 7zip, tar, and make in order to manually install a program.

  1. This walk though is done completely from the terminal command line. You can do a Ctrl+Alt+F2 to switch to TTY2 terminal mode (non-graphical) or go back to graphical mode (Ctrl+Alt+F7) and open a terminal.
  2. Services are programs that work across networks and map to certain port numbers. To see a listing of locally known services mapped to their known port numbers type in this command which will output the file to the terminal piped to more to see one screen at a time (press spacebar):
    cat /etc/services | more
  3. We will use a copy of the services file to practice using compressing and decompressing utility programs. Copy your services file to your home directory and make a notation of how many kilobytes the services file is. In the terminal type in the following commands:
    cp /etc/services ~
    ls -l
  4. To compress the file use gzip in verbose mode. Afterward, do a “ls -l” and you should see a file called services.gz and its size should be 60% of the original services file size.
    gzip -v services
    ls -l
  5. Now decompress the services.gz file by using gunzip (Note: if you try to decompress a file to a file name that already exists you will be prompted for permission to overwrite the file).
    gunzip -v services.gz
  6. You can also try a different compression utility like bzip2. Type in this command followed by a ls -l to check the file size. What file extension does a file compressed with bzip2 have? Which utility had a greater compression ration gzip or bzip2?
    bzip2 -v services
    ls -l
  7. Now decompress the file using bunzip2 (Note: if you try to decompress a file to a file name that already exists you will be prompted for permission to overwrite the file).
    bunzip2 -v services.bz2
  8. In addition to compressing files we can grab screen output and save and compress it to a file all in one command. For instance, if we wanted to see a list of all the processes (programs and daemons) currently running on our system. We could type in the following ps command piped (|) to more to see one screen at a time:
    ps -ef | more
    If you want to put that screen output into a file all in one command type in the following command which runs a “ps” piped to a “gzip” and output to a file using the standard output character > operator
    ps -ef | gzip -v > processes.gz
  9. Now decompress and view the file one screen at a time (Note: if you try to decompress a file to a file name that already exists you will be prompted for permission to overwrite the file):
    gunzip -v processes.gz
    cat processes | more
  10. Currently, the 7zip utility is supposed to have a great compression ratio. Lets give it a try. Type in the following command to install 7zip:
    sudo apt-get install p7zip-full
  11. Now you will run 7zip on the processes file. First run a ls -l to verify that there is in fact a file called processes and it is not still compressed in the .gz format. If you have the file present in the directory your can run 7zip on it by using the following command:
    7z a processes.7z processes
    Command breakdown:
    7z a (“a” adds to to an archive),
    processes.7z (the ouput file compressed),
    processes (the file or directory to compress)
  12. Do an “ls -l” command and you will see that the processes.7z file is signicantly smaller than the previous gzip and bzip2 versions.
  13. To extract in 7zip use the “x” function instead of the “a”. Use the following example (Note: if you try to decompress a file to a file name that already exists you will be prompted for permission to overwrite the file):
    7z x processes.7z
    or
    7z x processes.7z /home/your-username/name-of-the-decompressed-file
  14. To make a tar file out of the processes file you could type in the following command with the new file output name listed before the file to be used:
    tar -cvf processes.tar processes
    tar -xvf processes.tar (will extract the archive)
    The creation of the .tar file above is useful in that it is a backup of the original file, but it is not compressed. To do the same process and include gzip compression use the following command and the result will be a .tar.gz file:
    tar -zcvf processes.tar.gz processes
    tar -zxvf processes.tar.gz (will extract and and decompress the archive)
    The creation of the .tar.gz file is more useful in that it was also compressed. It is even more useful to use the tar utility to compress and entire folder:
    tar -cvf myHomeDir.tar *  (will archive every file in the current directory to an archive called “myHomeDir.tar”)
    tar -zcvf myHomeDir.tar.gz *  (will archive and compress every file in the current directory to an archive called “myHomeDir.tar”)
    Since I am in my home directory I can also tar a folder like my “Downloads” folder:
    tar -zcvf Downloads.tar.gz Downloads
    Now you should have a compressed archive called Downloads.tar.gz in your Home directory. To extract and decompress it to a folder called test use the following commands:
    mkdir test
    cd test
    tar -zxvf ../Downloads.tar.gz
  15. Congratulations! you have now used gzip, bzip2, 7zip and tar.

Author: Dan

Dan teaches computer networking and security classes at Central Oregon Community College.

Leave a Reply

Your email address will not be published. Required fields are marked *