Archive & Compress files in Linux

Aniket MoreAniket More
5 min read

In the last week, I started learning about the files permissions and the operations that can be performed on files. The next step in this journey, is learning about archiving and compressing files or directories on a Linux system. In this article, I will be sharing some commands and how I used them to archive & compress files.

To learn the basic operations that can be performed on the file, you can checkout this article :

Basic Linux Commands Everyone should know!

To learn file permissions, make sure you checkout this article :

Linux File Permissions : A Simple Guide

Archiving

Archiving simply means to combine multiple files and directories into a single file. This is mainly done to keep the backup of multiple files into a single file. It also makes distribution of files easier as multiple files and directories are shared as a single file.

To archive a file tar command is used in Linux. When a archived file is created it has .tar extension. For example : aniket-archive.tar , command-files.tar

Use this format to execute tar command :

tar <options> <archive-file.tar> <file/dir-to-be-archived>

tar -cvf test_archive.tar test_dir

Let's break down this command:

  • tar: The command we're using to create the archive.

  • -c: This option tells tar to create a new archive.

  • -v: This stands for "verbose". It prints the names of the files it's adding to the archive. This is optional but helpful to see what's happening.

  • -f: This option is followed by the name of the archive file we want to create.

  • test-archive.tar: This is the name we're giving to our new archive file. The .tar extension is conventional for creating tar archive files.

  • test-dir: This is the directory we're packaging into the archive.

I executed this command and here are some screenshots you can refer :

  • First I created a directory and added subdirectories and files into it.

  • Then executed the tar command :

To see the content of the archive file, I executed tar command with different options :

tar -tvf test-archive.tar

Let’s break down this command :

  • tar: The command we used to create the archive.

  • -t: This option gives the contents of archived file.

  • -v: This stands for "verbose". It prints names of files with detailed information of each file that is archived.

  • -f: This option is followed by the name of the archive file whose contents we want to see.

  • test-archive.tar: This is the name of the archive file with conventional .tar extension.

This is what I saw after executing this command :

Now, let’s say I want to extract all the files that I did archive into a new directory. This is how to do it :

tar -xvf test-archive.tar -C extract-dir

Let’s learn about this command in depth :

  • tar: The command we're using to extract the archive.

  • -x: This option tells tar to extract files from an archive.

  • -v: This makes verbose operation to show each file as it's extracted.

  • -f: This is followed by the name of the archive file we want to extract from.

  • -C extract-dir: This option tells tar to change to the extract-dir directory before extracting files.

This is how it looked when I executed above command :

Compressing

Compressing simply means to reduce the size of a file or an archived file. This is done using algorithms that find commonalities and remove redundant data, resulting in the smaller size of a file.

I learnt to use gzip command to archive files. Other commands that can be used are - bzip, xz .

When a file is compressed it’s size is smaller than the original file, but it needs to be decompressed before it’s use. You’ll see .gz extension for the compressed file. But when an archived file is compressed, it’s extension is .tar.gz .

I used this command to compress file :

gzip test-archive.tar

It generates a compressed file named - test-archive.tar.gz

Using -lh option with ls command will give detailed information about it and this is how it looks :

I checked the original file size and compressed archive file and definitely compressed archive file is smaller than the original file/dir.

Now, we have a file/directory that is first archived and compressed later. To extract the contents of this file, you can use this command :

tar -xvzf test-combined.tar.gz -C extract-combined

Let’s learn about how it works :

  • tar: The command we're using to extract the archive.

  • -x: This option tells tar to extract files from an archive.

  • -z: This option is needed because we're dealing with a gzip-compressed file.

  • -v: This makes the operation verbose, showing us each file as it's extracted.

  • -f: This is followed by the name of the archive file we want to extract from.

  • -C extracted: This option tells tar to change to the extracted directory before extracting files.

I was a bit curious about how other commands than gzip or tar can be used to compress a file / directory. While experimenting and learning more about compressing, I came across zip command which also can be used for compressing.

  • zip command to compress :
zip -r test_zipfile.zip test-dir

-r option is used to zip all subdirectories and files inside of test-dir.

  • unzip compressed :
unzip -d unzipped_file test_zipfile.zip

-d option is followed by the name of directory in which you want to extract decompressed files.

It looks something like this :

Summary

So, this is what I learnt about archiving and compressing :

  • Archiving - Combining multiple files & directories into a single file. tar command is used for it. Archived file has .tar extension by convention. It makes back up and distribution of files easier.

  • Compressing - Reducing the size of a file or directory. I used gzip, zip commands for it. Also tar command can be used to create a compressed archive of a file. It is useful for transmission of file. Compressed file size is always smaller than original file size.

0
Subscribe to my newsletter

Read articles from Aniket More directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aniket More
Aniket More

Hi, I'm Aniket. I'm passionate about coding responsive and dynamic web applications using React, TypeScript, HTML, and CSS. Proficient in Core Java and OOP concepts. Currently exploring Linux and diving into the exciting world of DevOps. Sharing my learning journey and projects to inspire others.