Home » Linux » Compressing and archiving files in Linux Operating System

Compressing and archiving files in Linux Operating System

Update on:
May 17, 2021

There is a difference between a compressed and an archive file. So, what is an archive file? It is a collection of files and directories that are stored in a single file. An archive file is not a compressed file.

What is a compressed file? This is a collection of files and directories stored in one file. It uses less disk space for storage.

In this article, we are going to discuss two compression tools:

  • bzip2
  • zip

Prerequisites

You should have files to archive and compress.

How to do compress

First, let’s see how we can compress the files:

  • We are going to look at compression with the help of bzip2. Decide which file to compress and run the following command:
$ bzip2 filename

Using bzip2, we can compress multiple files and directories at the same time. Just separate them by putting a space between each of them. You can compress it as follows:

$ bzip2 file1 file2 file3 /student/work
  • We are going to use zip for compression. By using the zip compression tool, files are compressed individually:
$ zip -r file_name.zip files_dir

After running the preceding command, the file to be compressed or the directory to be compressed, such as files_zip will be compressed and you will get a compressed file called file_name.zip. The -r option includes all the files in the files_dir directory recursively.

Now, let’s discuss how to archive files:

An archive file contains more than one file or directory. Tar is used to archive files without compression.

The following are the two archiving modes:

  • -x: extract an archive
  • -c: create an archive

Options:

  • -f: FILE name of the archive—you must specify this unless using tape drive for archive
  • -v: Be verbose, list all files being archived/extracted
  • -z: Create/extract archive with gzip/gunzip
  • -j: Create/extract archive with bzip2/bunzip2
  • -J: Create/extract archive with XZ

To create a TAR file, use the following command:

$ tar -cvf filename.tar directory/file

In this example, we are representing a file, which is created when we use the tar command. Its name is filename.tar. We can specify the directory or file in which we are putting archive files. We must specify it after inserting filename.tar. Multiple files and directories can archive at the same time, just leave a space between the file and directory names:

$ tar -cvf filename.tar /home/student/work /home/student/school

After executing the preceding command, all the contents from work and school directories will be stored in the new file filename.tar

After running the preceding command, all the contents from work and school subdirectories will be stored in the new file filename.tar.

The following command is used to list the contents of a TAR file:

$ tar -tvf filename.tar

To extract the contents of a TAR file, use the following command:

$ tar -xvf filename.tar

This command is used for extraction and it will not remove the TAR file. However, in your current working directory, you will find the unarchived contents.

How it works

bzip2 is used to compress the files and folder at the same time. Just issue the command in the terminal and the files will be compressed. After the compression, the compressed file will get the extension .bz2.

zip compress the files individually and then collects them in a single file.

A TAR file is a collection of different files and directories, which combines them into one file. TAR is used for creating backups and archives.

Related Posts

Creating a lame utility HTTP server in Linux Operating System

In this article, we will discuss the cURL tool in Linux. The cURL tool is used for transferring the data from or to a server. It supports many protocols, and http is one of them. cURL is used to transfer the data from URL. It has so many tricks to offer, such as http...

Finding binary dependencies in Linux Operating System

In this article, we are going to check the executable. We will find out which string is present in it by using the string command.PrerequisitesBesides having a terminal open, make sure you have a binary present in your directory.Find dependencies First, we...

Capturing network traffic headlessly in Linux Operating System

In this article, we are going to learn how to capture traffic. We are going to capture network traffic with a packet sniffer tool called tcpdump. This tool is used to filter or capture TCP/IP packets that are transferred or received over a network.PrerequisitesBesides...

Follow Us

Our Communities

More on Linux

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!

0 Comments

0 Comments

Submit a Comment

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

seventeen − 10 =

Shares