How to archive and unzip files in Linux: Tar utility

  • Last updated: Nov 27, 2024
  • Views: 36
  • Author: Admin

Archiving and unarchiving files is one of the main tasks faced by system administrators, devops engineers or ordinary Linux users. The tar utility is a powerful tool for creating archives, unarchiving them and managing their contents. The tar utility is widely used due to its flexibility, support for various formats and compatibility with other utilities such as gzip and bzip2 for data compression.

In this article, we'll look at how to use tar correctly to quickly and efficiently work with archives in Linux, whether it's backing up data, transferring files, or structuring them.

 

Contents of the article:

  1. Syntax.
  2. Create a tar archive.
  3. Unzip the tar archive.
  4. View archive contents.

 

1. Syntax:

tar [options] [file]

Parameters:

  • -c : Create an archive.
  • -x : Extracts the archive.
  • -f : Create an archive with the given file name.
  • -t : Show or list files in an archived file.
  • -u : Archives and appends to an existing archive file.
  • -v : Display detailed information.
  • -A : Combines archive files.
  • -z : Create a tar file using gzip compression.
  • -W : Checks the archive file.

 

2. Create an archive.

In the first example we will create a tar archive named archive.tar.gz and put five sql files into this archive.

$. tar -czvf archive.tar.gz  file_1.sql file_2.sql file_3.sql file_4.sql file_5.sql

linux command tar

As a result, an archive.tar.gz file should be created with our sql files.

 

In the second example we will create a tar archive using the bzip2 compression algorithm, for this we need to use the -j parameter.

$. tar -cjvf archive.tar.bz2 file_1.sql file_2.sql file_3.sql file_4.sql file_5.sql

linux command tar

As a result, the archive.tar.bz2 file should be created.


 

3. Unzip the tar archive.

To unzip our archives now, we need to use the same command, but with the additional parameter -x.

Unzipping the .tar.gz archive

$. tar -xvf archive_gz.tar.gz

linux command tar

 

Unzipping the .tar.bz2 archive

$. tar -xvf archive_bz.tar.bz2

linux command tar


 

4. View archive contents.

In the next two examples we will only look at what we have in the archives without unpacking them, for this we need to use the -t parameter.

Show contents of .tar.gz archive

$. tar -tvf archive_gz.tar.gz

linux_command_tar

 

Show contents of archive .tar.bz2

$. tar -tvf archive_bz.tar.bz2

linux command tar


 

In short, the tar utility is an indispensable tool for working with archives in Linux. It allows you to efficiently archive files, compress them to save space, and easily restore data from archives. Once you master basic commands such as creating, unpacking, and viewing archive contents, you can simplify file management and automate many routine tasks. Whether you are working with backups, large amounts of data, or simply transferring files, tar will become your reliable assistant.

Experiment with the options and expand your skills to use this tool to its full potential!

 

SIMILAR ARTICLES