Skip to main content

Command Palette

Search for a command to run...

File Archiving & Compression

Understanding gzip, zip/unzip and tar for File Compression

Updated
โ€ข3 min read
File Archiving & Compression
S

Growing in DevOps, together! ๐Ÿค | Associate Software Engineer at Tech Mahindra | Enthusiastic about automation, cloud solutions, and efficient software delivery. | Let's connect, collaborate, and learn from each other!

1. tar (Tape Archive)

tar is used for archiving files (bundling them together without compression).

Archiving a file means combining multiple files or directories into a single file without compressing them. This makes it easier to store, transfer, and back up data.

๐Ÿ”น Example: Imagine you have 100 files in a folder. Instead of copying each file separately, you can archive them into one file (backup.tar), making it easier to manage.

Basic Syntax

tar [options] archive_name.tar files_or_directory

Common tar Commands

CommandDescription
tar -cvf archive.tar file1 file2 dir1Create an archive (.tar file)
tar -xvf archive.tarExtract an archive
tar -tvf archive.tarView contents of an archive
tar -cvzf archive.tar.gz dir1Create a compressed archive with gzip
tar -xvzf archive.tar.gzExtract a .tar.gz file
  1. Create a .tar Archive

     tar -cvf archive.tar file1 file2 
     tar -cvf backup.tar /home/user/Documents
    

    c โ†’ Create a new archive

    v โ†’ Verbose (lists files being added)

    f โ†’ Specify the output filename (archive.tar)

  2. Extract a .tar Archive

     tar -xvf archive.tar
     tar -xvf archive.tar -C /path/to/destination # extracting to a specific directory
    

    x โ†’ Extract files

    v โ†’ Show extraction process

    f โ†’ Specify archive file

  3. View Contents of a .tar Archive

     tar -tvf archive.tar
    

    t โ†’ List archive contents without extracting


    2. gzip / gunzip

    gzip - It is used for compressing files using the .gz format.

    gunzip - It is used to decompress files that were compressed using gzip. It restores the original file by removing the .gz extension.

    1. To Compress a file - gzip

       gzip file_name
      
  • The original file is removed after compression.

  • It replaces the original file with a compressed version to save disk space.

    1. To unzip a file

       gunzip file.txt.gz
      


3. zip and unzip

zip is used to compress multiple files into one .zip file, and unzip is used to extract it.

        zip archive_name.zip file1 file2
        unzip archive_name.zip

Common zip/unzip Commands

CommandDescription
zip files.zip file1 file2 dir1Create a .zip archive
unzip files.zipExtract a .zip file
zip -r files.zip dir1Zip a directory recursively
unzip -l files.zipList contents of a .zip file
unzip -d target_dir files.zipExtract files to a specific directory


Difference Between tar, gzip, and zip

Featuretargzipzip
Archivingyesnoyes
Compressionno (unless used with -z)yesyes
Multiple Filesyesno (single file only)yes
Format.tar or tar.gz.gz.zip
Extract Commandtar -xvfgunzipunzip

Using tar with Compression

  1. Create a Compressed .tar.gz Archive
tar -czvf archive.tar.gz file1 file2 dir/
  • z โ†’ Compress using gzip

  1. Extract a .tar.gz Archive

     tar -xzvf archive.tar.gz
    
    • x โ†’ Extract

    • z โ†’ Decompress gzip

Note : tar.gz and tgz is the same thing.