File Archiving & Compression
Understanding gzip, zip/unzip and tar for File Compression

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
| Command | Description |
tar -cvf archive.tar file1 file2 dir1 | Create an archive (.tar file) |
tar -xvf archive.tar | Extract an archive |
tar -tvf archive.tar | View contents of an archive |
tar -cvzf archive.tar.gz dir1 | Create a compressed archive with gzip |
tar -xvzf archive.tar.gz | Extract a .tar.gz file |
Create a .tar Archive
tar -cvf archive.tar file1 file2 tar -cvf backup.tar /home/user/Documentsc โ Create a new archive
v โ Verbose (lists files being added)
f โ Specify the output filename (archive.tar)

Extract a .tar Archive
tar -xvf archive.tar tar -xvf archive.tar -C /path/to/destination # extracting to a specific directoryx โ Extract files
v โ Show extraction process
f โ Specify archive file


View Contents of a .tar Archive
tar -tvf archive.tart โ 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 usinggzip. It restores the original file by removing the.gzextension.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.

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
| Command | Description |
| zip files.zip file1 file2 dir1 | Create a .zip archive |
| unzip files.zip | Extract a .zip file |
| zip -r files.zip dir1 | Zip a directory recursively |
| unzip -l files.zip | List contents of a .zip file |
| unzip -d target_dir files.zip | Extract files to a specific directory |


Difference Between tar, gzip, and zip
| Feature | tar | gzip | zip |
| Archiving | yes | no | yes |
| Compression | no (unless used with -z) | yes | yes |
| Multiple Files | yes | no (single file only) | yes |
| Format | .tar or tar.gz | .gz | .zip |
| Extract Command | tar -xvf | gunzip | unzip |
Using tar with Compression
- Create a Compressed .tar.gz Archive
tar -czvf archive.tar.gz file1 file2 dir/
- z โ Compress using gzip
Extract a .tar.gz Archive
tar -xzvf archive.tar.gzx โ Extract
z โ Decompress gzip
Note : tar.gz and tgz is the same thing.




