Skip to main content

One post tagged with "tar"

View All Tags

Working with TAR Archives in Linux Command Prompt

· 2 min read
Customer Care Engineer

The TAR format is extremely popular in the Linux world and is the de facto data archiving standard. It can’t compress files by itself but perfectly cooperates with such compression utilities as gzip or bzip2. Therefore, most archives packed with this format that you can find on the web will look as archive_name.tar.gz.

Before You Begin

In most cases, tar is installed by default. To be 100% sure, run the command to install this archiver:

For Debian and Ubuntu:

sudo apt update && sudo apt install tar

For CentOS and Rocky Linux/AlmaLinux:

sudo yum makecache && sudo yum install tar

How to Create a TAR Archive

Without compression:

tar -cvf archive.tar /file/path

Where:

  • -c — to create an archive
  • -v — show details in the terminal (you’ll find it useful if you want to see what’s going on)
  • -f — specify the name of the archive file

With additional compression (for example, gzip):

tar -czvf archive.tar.gz /file/path
  • -z — adds gzip compression.

The second command is preferable in most cases, since additional compression will save time for downloading or uploading a file from or to the server, and the archive will take up less drive space.

How to extract a TAR Archive

For gzip:

tar -xzvf archive.tar.gz

For bzip2:

tar -xjvf archive.tar.bz2

For a uncompressed archive:

tar -xvf archive.tar

Additional Useful Options

  • -t — view the archive contents without unpacking:
tar -tvf archive.tar

Shows a list of archive files in the console, but doesn’t unpack it.

  • -u — update files within the archive:
tar -uf archive.tar /path/to/new_files

Consider the following details when updating a file within the archive:

  • If the archive contains no new_file.txt, it will be added.
  • If the archive already contains a file of the same name, but its contents on the drive have changed, then this file will be updated to the latest version.

You can also use this command to update multiple files at once, for example:

tar -uf archive.tar /path/to/new_files/*.txt

This command will update all txt files in the archive and will add new ones if they haven’t been added before.