More

    File compression using tar with gzip, bzip2, and xz

    The tar command

    Archiving and compressing files are useful when creating backups and transferring data across a network. One of the oldest and most common commands for creating and working with backup archives is the tar command. tar  stands for “tape archive,” a utility that is used to bundle multiple files and directories together into a single archive file. With tar, users can gather large sets of files into a an archive. A tar archive is a structured sequence of file data mixed in with metadata about each file and an index so that individual files can be extracted. The archive can be compressed using gzip, bzip2, or xz compression. However, tar alone merely packages files together; it’s the compression algorithms like gzip,bzip2 , and xz that turn tar into a potent file compression tool. In this blog, we’ll explore the world of file compression using tar with these three methods and learn how to wield their power effectively.

    tar command Options

    The tar command expects one of the three following options:

    1. -c or --create option to create an archive.
    2. -t  or --list option to list the contents of an archive.
    3. -x  or --extract option to extract an archive.
    4. -f  or --file= option with a file name as an argument of the archive to operate.

    5. -v  or --verbose option for verbosity;

     

    File Compression Using tar with gzip, bzip2, and xz

    The tar command supports three compression methods.

    tar Compression Options

    1. Gzip: Fast and Efficient Compression

    The gzip compression is the fastest and oldest one and is most widely available across distributions and even across platforms. It is known for its speed and efficiency. It uses the DEFLATE compression algorithm to shrink file sizes while maintaining a good balance between compression speed and compression ratio.

    Compress using Gzip

    Here’s how you can use gzip with tar :

    -z or --gzip  filename.tar.gz or filename.tgz

    To create a gzip compressed archive named /root/etcback.tar.gz, with the contents from the /etc directory on a host:

    # Compress using gzip
    [root@rocky9 ~]# tar -czf /root/etcbackup.tar.gz /etc
    tar: Removing leading /' from member names
    
    </pre>
    <h4>Extract Gzip Archive</h4>
    To decompress the file that we compressed above(<i>/root/etcbackup.tar.gz</i>) with <em>gzip </em>in the <strong><em>/tmp/etc_backup</em></strong> directory, use the following command:
    <pre class="EnlighterJSRAW" data-enlighter-language="generic">[root@rocky9 ~]# mkdir /tmp/etc_backup
    [root@rocky9 ~]# cd /tmp/etc_backup
    [root@rocky9 etc_backup]# tar -xzvf /root/etcbackup.tar.gz
    etc/
    etc/mtab
    etc/fstab
    etc/crypttab
    etc/lvm/
    etc/lvm/devices/
    etc/lvm/devices/system.devices
    etc/lvm/archive/
    
    </pre>
     
    <h3>2. Bzip2: High Compression Ratio</h3>
    <p class="p1"><code class="EnlighterJSRAW" data-enlighter-language="generic">bzip2</code> , short for "<em>Burrows-Wheeler block-sorting file compressor</em>," is another compression utility that excels in providing high compression ratios. It achieves this by using the <em>Burrows-Wheeler transform</em> and <em>Run-Length Encoding</em>. While <em><strong>bzip2</strong></em> might be slower than <em>gzip</em>, its superior compression ratio often justifies the extra time.</p>
    
    <h4>Compress using Bzip2</h4>
    Here's how you can use <em>Bzip2</em> with <code class="EnlighterJSRAW" data-enlighter-language="generic">tar</code> :
    
    <code class="EnlighterJSRAW" data-enlighter-language="generic">-j</code>  or <code class="EnlighterJSRAW" data-enlighter-language="generic">--bzip2</code> <em>filename.tar.bz2</em>
    
    Confirm Bzip2 is installed on your system. If not you can install it:
    <pre class="EnlighterJSRAW" data-enlighter-language="generic">[root@rocky9 ~]# yum install bzip2
    Rocky Linux 9 - AppStream                                                                                                                                                             4.6 kB/s | 4.5 kB     00:00    
    Dependencies resolved.
    ======================================================================================================================================================================================================================
     Package                                          Architecture                                      Version                                                   Repository                                         Size
    ======================================================================================================================================================================================================================
    Installing:
     bzip2                                            x86_64                                            1.0.8-8.el9                                               baseos                                             52 k
    
    Transaction Summary
    ======================================================================================================================================================================================================================
    Install  1 Package
    
    Total download size: 52 k
    Installed size: 94 k
    Is this ok [y/N]: y
    Downloading Packages:
    bzip2-1.0.8-8.el9.x86_64.rpm                                                                                                                                                          
                                                                                                                                                                      1/1 
      Verifying        : bzip2-1.0.8-8.el9.x86_64                                                                                                                                                                     1/1 
    
    Installed:
      bzip2-1.0.8-8.el9.x86_64                                                                                                                                                                                            
    
    Complete!
    </pre>
     
    
    Here's how to use <strong><em>Bzip2</em></strong> with <code class="EnlighterJSRAW" data-enlighter-language="generic">tar</code> to create a <em>bzip2</em> compressed archive named <strong><em>/root/varlogauditbackup.tar.bz2</em></strong>, with the contents from the <em>/var/log/audit</em> directory:
    <pre class="EnlighterJSRAW" data-enlighter-language="generic">[root@rocky9 ~]# tar -cjf /root/varlogauditbackup.tar.bz2 /var/log/audit
    tar: Removing leading /' from member names
    

     

    Extract Bzip2 Archive

    To extract the file that we compressed above(/root/varlogbackup.tar.bz2) with Bzip2 in the /tmp/logbackup directory, use the following commands:

    [root@rocky9 ~]# mkdir /tmp/logauditbackup
    [root@rocky9 ~]# cd /tmp/logauditbackup
    [root@rocky9 logbackup]# tar -xjvf /root/varlogauditbackup.tar.bz2 
    var/log/audit/
    var/log/audit/audit.log.4
    var/log/audit/audit.log.3
    var/log/audit/audit.log.2
    var/log/audit/audit.log.1
    var/log/audit/audit.log
    
    

     

    3. Xz: Maximum Compression

    xz is the heavyweight champion of compression when it comes to squeezing the last bit of space out of your files. It employs the LZMA2 compression algorithm, offering an excellent balance between high compression ratios and reasonable compression speeds.

    Compress using Xz

    Here’s how you can use Xz with tar :

    -J or -xz filename.tar.xz

    To create a xz compressed archive named, /root/sshconfig.tar.xz, with the contents from the /etc/ssh directory, this how to do it:

    [root@rocky9 ~]# tar -cJf /root/sshconfig.tar.xz /etc/ssh
    tar: Removing leading `/' from member names
    

    Lets verify the contents of the xz archive using the tf options of the tar command:

    [root@rocky9 ~]# tar -tf /root/sshconfig.tar.xz 
    etc/ssh/
    etc/ssh/moduli
    etc/ssh/ssh_config
    etc/ssh/ssh_config.d/
    etc/ssh/ssh_config.d/50-redhat.conf
    etc/ssh/sshd_config
    etc/ssh/sshd_config.d/
    etc/ssh/sshd_config.d/50-redhat.conf
    etc/ssh/sshd_config.d/01-permitrootlogin.conf
    etc/ssh/sshd_config.d/01-complianceascode-reinforce-os-defaults.conf
    etc/ssh/sshd_config.d/00-complianceascode-hardening.conf
    etc/ssh/ssh_host_ed25519_key
    etc/ssh/ssh_host_ed25519_key.pub
    etc/ssh/ssh_host_ecdsa_key
    etc/ssh/ssh_host_ecdsa_key.pub
    etc/ssh/ssh_host_rsa_key
    etc/ssh/ssh_host_rsa_key.pub

     

    Extract XZ Archive

    To extract the file that we compressed above(/root/sshconfig.tar.xz) with xz in the /tmp/sshbackup directory, use the following commands:

    Decompress using xz

    [root@rocky9 ~]# mkdir /tmp/sshbackup
    [root@rocky9 ~]# cd /tmp/sshbackup
    
    # Decompress using xz
    [root@rocky9 sshbackup]# tar -xJvf /root/sshconfig.tar.xz 
    etc/ssh/
    etc/ssh/moduli
    etc/ssh/ssh_config
    etc/ssh/ssh_config.d/
    etc/ssh/ssh_config.d/50-redhat.conf
    etc/ssh/sshd_config
    etc/ssh/sshd_config.d/
    etc/ssh/sshd_config.d/50-redhat.conf
    etc/ssh/sshd_config.d/01-permitrootlogin.conf
    etc/ssh/sshd_config.d/01-complianceascode-reinforce-os-defaults.conf
    etc/ssh/sshd_config.d/00-complianceascode-hardening.conf
    etc/ssh/ssh_host_ed25519_key
    etc/ssh/ssh_host_ed25519_key.pub
    etc/ssh/ssh_host_ecdsa_key
    etc/ssh/ssh_host_ecdsa_key.pub
    etc/ssh/ssh_host_rsa_key
    etc/ssh/ssh_host_rsa_key.pub
    
    
    
    

     

    Conclusion

    In the world of file compression, the tar utility serves as the foundation upon which the power of gzip, bzip2, and xz is harnessed. Each compression method has its strengths and weaknesses, allowing you to tailor your approach based on your specific needs.

     

    Read More:

    Recent Articles

    Related Articles

    Leave A Reply

    Please enter your comment!
    Please enter your name here