Archiving Files Using Linux Command Line

In this article, we will understand the basics of Archiving files using the command line on Linux. By Himanshu Bhatt, on December 24, 2018

As we already understand what Compression (Compression techniques in Linux) is? We shall learn about Archives. We prefer compression as it is convenient to send file compressed through a network but sometimes it is not a smart way to compress the multiple files separately but as a single entity, we can sure compress multiple files in a single line but in the core, all files are compressed independently.

What is Archiving in Linux?

Archiving is the solution to this problem. The UNIX utility to archive files is tar, that is TApeaRchive.

Modes of TAR

Tar has 3 modes those are:

  1. Create: which is a new archive from a group of files
  2. Extract: means getting one or more files from the archive
  3. The list: which shows the content of the archive without extracting it.

Our operations and command for archiving includes above three modes and will discuss throughout this article.

A tar file is also called a tarball, now let's dive into the practical thing.

Example 1: Create Mode

Archives in Linux 1


Creating a Tarball(at least) requires 2 options, here we used 'c' and 'f' :

  • c: create an archive file
  • f: tells tar to expect a file name as next argument.

In line 1, we created the tar file with .tar extension and used a wildcard for all the files we have selected (multiple file name or both can be used).

tar command consists of:

tar [options] tarname filename1 filename2 ...

The first option of tar always is its mode, here it was created ('c') then 'f' where we pass file names after the name ofthe tar file.

In line 2, the size of includehelp.tar is slightly more than the files it contains, but it can be compressed either explicitly compressing it with gzip(or bzip2) or with tar itself.

Example 2: Compressing

Archives in Linux 2


In the above example, we used 3 option format of tar, for compression with achieving.

In line 2, again the first option is to create mode, second one 'z' is for gzip (we can use 'j' for bzip2), and third for file argument. Format of compress tar with gzip is .tar.gz or .tgz(for bzip2, format will be or tbz).

Example 3: List mode

Archives in Linux 3

In the above sample, the command used is:

    tar –tzvf  filename

Where, –t option is used for list files in archive and z for gzip (j for bzip2) and v is verbose that is whatever is happing will shown on screen and f for given filename includehelp.tar.gz (or .tbz).


Although the position of the option doesn't matter in Linux command line in case of tar command, we always need to keep –f option at the end because after f it expects a filename.

Recommended Articles:

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.