Compressing and archiving files in Linux Operating System

February 19, 2021

Comments

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.

Satish Kumar

Satish Kumar

I am Satish Kumar, Founder of LinuxConcept. Linux and F.O.S.S enthusiast, love to work on open source platform and technologies.

Related Posts

Encrypting/decrypting files from a script in Linux

In this article, we are going to learn about OpenSSL. In this section, we are going encrypt and decrypt messages and files using OpenSSL.Prerequisites Besides having a terminal open, you need to have a basic knowledge of encoding and decoding schemes....

0 Comments

0 Comments

Submit a Comment

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

eighteen + two =

News & Updates

Join Our Newsletter