Archiving files in Linux. Creating and Extracting Archives Using the Tar Command in Linux Tar Archive

Most of the files for Linux that are downloaded from the Internet are archived in tar, tar.gz, tar.bz2 formats and it is important to know how to extract them. This article shows how to extract (unzip) and unzip (untar) files and folders from tar, tar.gz and tar.bz2 files from the command line in Linux. You will also learn how to view the contents of a tar archive without unpacking it and how to extract only a single file or a single folder. tar itself is not an archiver in the usual sense of the word, because it does not use compression itself.

Tar is a more common archiver used on Linux systems.

After this, the resulting *.tar file is compressed with a program, for example, gzip. This is why archives usually have the extension .tar.gz or .tar.bz2 (for gzip and bzip2 archivers, respectively) When large size backup file, it is not always necessary to unzip all the files; just one file or folder is enough.

In the version with Linux VPS based on OpenVZ and Xen, the basic components of the system are enough to work with archives, but only Tar, Gzip and Bzip2, which, when creating a compressed archive, work as a single whole, because The Tar archiver itself does not provide the ability to compress data. Gzip or Bzip2 is used for this purpose. Card files created using Gzip usually have the extension .tar.gz, and when using Bzip2 - .tar.bz2.

At the same time, many archivers (for example, Gzip or bzip2) do not know how to compress multiple files, but work only with one file or input stream. Therefore, most often these programs are used together. tar creates an uncompressed archive that contains selected files and directories, while preserving some of their attributes (such as permissions).

To unpack the tar.gz archive into the current directory you need to run the command:

tar -xvzf archive.tar.gz

x - allows you to extract files from the archive.
v - Makes tar output verbose. This means that all files found in the archive will be displayed on the screen. If this option is omitted, the information output during processing will be limited.
f is a required option. Without it, tar tries to use magnetic tape instead of the card file.
z - allows you to process a gzip-compressed archive (with the .gz extension). If you forget to specify this function, tar will throw an error. Conversely, this option should not be used for uncompressed archives.

A sign is not at all necessary for tar!

Unpack the tar file:

tar -xvf foo.tar

Unpack and unzip the tar.gz file:

tar -xvzf foo.tar.gz

Unpack and unzip the tar.bz2 file:

tar -xvjf foo.tar.bz2

Creating a tar archive without compression in Linux

To create such an archive, use the command:

tar -cf filename.tar file1 file2 fileN

The weather parameter -cf is responsible for creating a filename.tar archive, which will include specified files. Instead of files, directories can also be specified.

Creating an archive using compression in Linux

Because there are at least two methods available for contraction, each method uses a separate set of parameters.

Gzip compression sample:

tar -cvzf filename.tar.gz dir_name

In this case, we tried to pack the dir_name folder into the filename.tar.gz folder. Of the specified parameters, -z indicates the use of the Gzip method, and -v displays the results of the archive creation process, indicating the packed files or folders.

Viewing archive contents without unpacking in Linux

For these purposes the following command is used:

tar -tf filename.tar.gz

As a result of executing the command, a list of files and folders stored in the archive will be displayed. If you add -v to the list of parameters, you will get an expanded output of information about the contents, indicating the file size, access rights, names of owners and their groups, etc.

Other features of tar on Linux

tar provides many useful features. For example, it is possible to specify files and directories that will not be included in the archive, add files ( named data area on a storage medium) to the existing archive ( 1) an institution or structural unit of an organization that carries out storage, acquisition, accounting and use of archival documents; 2) a collection of written monuments (manuscripts, letters, etc.)), take the list of objects to pack from text file and much more. As always, it will help you understand all the abundance of options.

The tar command line utility is used for archive services on UNIX-based operating systems. It can be used to archive data and improve disk space utilization.

Tar.gz is an archive created by the Tar program, and then zipped with the Gzip program. This double archiving is due to the fact that the Gzip program compresses well and quickly, but does it only with one file. Therefore, in order to pack several files, first one tar archive is created from them, and after that it is already packed with the Gzip program. Moreover, it is worth noting that the primary tar archive practically does not compress information, but only combines it into one file. In the final version, we get an archive with a double extension filename.tar.gz or filename.tgz

How to create a tar.gz archive correctly?

On Windows OS

You need to use the archiver 7-zip.

On Linux OS

We connect to the hosting server via the SSH protocol using the PuTTY program. IN command line enter:

This command will create a file.tar archive from all the files in the /full/path directory. But because tar does not archive, but only combines files, then we need to compress it again using Gzip:

tar -czvf file.tar.gz /full/path

As a result, we will get file.tar.gz which will be located in the /full/path directory.

tar command syntax rules

tar [-keys] [name of the archive that will be created] [what to pack\where to pack]

Regarding the keys:
c (create) - create an archive file

z (gzip) - archive a file using gzip

How to properly unpack a tar.gz archive?

On Windows OS

On Linux OS

On the hosting server - connect to the server via the SSH protocol using the PuTTY program. In the command line enter:

Syntax

tar [-keys] [archive to be unpacked or path to it]

x (eXtract) - unpack files
v (verbose) - show execution information
f (file) - indicates that you need to create a file with the name that is specified after the keys (in our example, file.tar or file.tar.gz), if you do not specify this key, the default name will be used or problems will arise.
file.tar.gz is the name of the archive that needs to be unpacked.

There is nothing better for remembering information than reading and then writing this information in a notebook, so I like to read and write information in my notebook website

Today we will talk about such a thing as archiving, namely packing and unpacking formats such as: tar, gzip, zip and rar in Unix and Linux operating systems

We will divide our article into sections for easy navigation.

Archiving in Linux

Linux or Linux is now the most popular system not only for system administrators, but also for the majority of ordinary citizens, and the reason is banal - the departure from Microsoft at the state level and the widespread introduction of “free software of Russian design”, but no matter what we call it, we all understand that the only free software that can replace the operating system for the average user system for office work is the Linux family. Therefore, as they say, you can already start studying it for everyone everywhere)))) but let’s not go away from the topic, but let’s get back to our sheep!

Although Linux has a graphical shell and you can even download graphic packages for working with the most popular archiver extensions, we will consider working with archives at the console level (but if anyone needs to, we will review packages for working not through the console)

Archiving Tar in Linux

Tar- or as we called it, it is not an archiver, the reason for this is the logic of the operation of this tool! it's just a packer, or to be more precise, tar's task is to create a container in tar format into which it places the files you select (without compression). But if you need to compress this data or compress it to reduce the size of the archive, then additional tools such as Gzip or bzip2 are used (you should have noticed that when downloading packages or files they had a strange name-file format .tar.gz) The whole point is that the principle of operation of the data compression tool only works in single-threaded mode, therefore, in order to archive many files and compress them, we needed to pack them into TAR (one file), and only then run them through Gzip or bzip2 so these tools work like Bonnie and Clyde)

Creating a TAR archive

First, let's start by packing the Tar archive in Linux/ubuntu, and for this you need to remember the pattern or command syntax:

tar [options]<имя файла.tar>

  • c (create) - create an archive
  • f (file) — specify the archive name
  • additionally using bzip2
  • additionally using gzip

To create a tar archive, we need only two options c and f, but if you want to see the process of packing files, you can add the v key and then you will see a running list of files on the screen (so to speak, an “analog” of Windows progress in the form of percentages)

tar -cvf newfile.tar /foto

in this command we indicated to create a newfile.tar archive and pack the entire foto folder

In the list of options, you noticed two keys j and z; these keys are used for compression into a gzip or bzip2 archive; if you need to archive with compression, then use the following command:

tar -cvjf newfile.tar /foto- will create an archive + compression newfile.tar.bz2
tar -cvzf newfile.tar /foto- will create an archive + compression newfile.tar.gz

Unpack TAR archive

Let's look at the command syntax to unpack or unzip a tar archive in linux/ubunta:

tar [options]<имя файла.tar>[files or folders that we will unpack]

Options for creating a tar archive:

  • x (extract) - extract files from the archive
  • f (file) — specify the archive name
  • j (bzip2) - compress/unpack archive additionally using bzip2
  • z (gzip) - compress/unpack archive additionally using gzip
  • v (verbose) - lists progress
  • C (upper case) - indicate an alternative location for unzipping files

tar -xvjf newfile.tar.bz2
tar -xvzf newfile.tar.gz -C /home/nibbl/desktop

General information on working with Tar

  1. tar --help — call help on commands and parameters
  2. man tar - calling extended documentation
  3. Official documentation for the tar command - link

Zip Archiving in Linux

The zip archiver is a star in the software world (well, not counting rar), this format is very popular for windows systems and by default it is already built into the clean assembly operating system, so only after installing Windows can you unzip files in zip format

but let's get down to business, in Linux and Unix systems things are different, this is the Tar format by default, but for zip you need to install the unzip package

Installation:

  1. sudo apt-get update && apt-get upgrade
  2. sudo apt-get install unzip

Creating a ZIP archive

First, let's remember the template or command syntax for creating a zip archive in Linux:

zip [options]<имя файла.zip>[files or folders that we will pack]

Options for creating a tar archive:

  • r (recurse) - recursive creation of an archive
  • s (size) - splitting the archive into a specific size k (kB), m (MB), g (GB) or t (TB)
    example: zip-s 300m<файл 1 ГБ>
    at the output we get:
    file.zip (300 mb, master file)
    file.001.zip (300 mb)
    file.002.zip (300 mb)
    file.003.zip (100 mb)
  • P (password) — password-protect the archive (you can use the key e then you will enter the password in a separate line with asterisks)
    example: zip -P mypassword -r file.zip ./home/nibbl/foto
    example2: zip -er file.zip ./home/nibbl/foto
  • x - exclude files or directories from the archive
  • 1-9 — compression level (where 1 is without compression, and 9 is the best compression)

zip –r -9 -P 123 — archive.zip /home/nibbl/desktop/myfile

— with this command we compressed the myfile folder, created an archive named archive.zip and set a password for the archive 123

Unpack zip archive

Let's look at the command syntax to unpack or unzip a tar archive:

unzip<имя файла.zip>

Options for creating a tar archive:

  • d (directory) - specify the directory for unzipping
  • l — display a list of files in the archive
  • d - delete a specific file or directory from an already created archive
  • v - shows detailed information by files in the archive ()

unzip<имя файла.zip> — unpack the archive into the category in which we are located
unzip<имя файла.zip>-d /home/nibbl/desktop- unpack the archive to your desktop

General information on working with Zip

  1. zip --help or unzip --help - call help on commands and parameters
  2. man zip or man unzip - call extended documentation
  3. Official documentation for the zip command - link

In this article, we'll show you how to use the Tar utility to extract and create tar archives through practical examples and detailed explanations of the most common Tar options.

What is Tar?

The tar command is used to create tar archives by converting a group of files into an archive. It also has the ability to extract tar archives, display a list of files included in the archive, add additional files to an existing archive, and various other types of operations.

Tar supports a wide range of compression programs such as gzip, bzip2, lzip, lzma, lzop, xz and compress. When creating compressed tar archives, it is common practice to add a compressor suffix to the archive file name. For example, if the archive was compressed using gzip, it would be named archive.tar.gz.

Tar was originally designed to create archives when storing files on magnetic tape, which is why it gets its name " T ape AR chive."

There are two versions of tar, BSD tar and GNU tar, with some functional differences. Majority Linux systems come with GNU tar preinstalled by default. If you don't have tar installed, we recommend installing it first by following our article.

Command line syntax

Before we move on to using the tar command, let's start by reviewing the basic syntax.

Tar

  • OPERATION – Only one argument is allowed and required. Most commonly used operations:
    • --create (-c) – Create a new tar archive.
    • --extract (-x) – Extract the entire archive or one or more files from the archive.
    • --list (-t) – display a list of files included in the archive
  • OPTIONS – Most commonly used operations:
    • --verbose (-v) – Show files processed by the tar command.
    • --file=archive=name (-f archive-name) – Specifies the name of the archive file.
  • ARCHIVE_NAME – Archive name.
  • FILE_NAME(s) – A space-separated list of file names to be extracted. If not provided, the entire archive will be extracted.

When executing the tar command, you can use the long or short form of tar operations and parameters. Long forms are more readable and short forms are faster. Long form options are prefixed with a double dash (--). Short form parameters are prefixed with a single dash (-), which can be omitted.

Creation of Tar Archive

Use the -c operator to create a tar archive. For example, to create an archive file named archive.tar from files named file1, file2, file3, run the following command:

Tar -cf archive.tar file1 file2 file3

Here is the equivalent command using long form parameters:

Tar --create --file=archive.tar file1 file2 file3

The following example will create a backup.tar archive from the /home/username directory:

Tar -cf backup.tar /home/username

You can create archives from the contents of one or more directories or files. By default, directories are archived recursively unless the --no-recursion option is specified. Use the -v option if you want to view the files that are being processed.

Creating a Tar Gz archive

Gzip is the most popular compression algorithm for tar files. When compressing tar archives with gzip, the archive name must end with either tar.gz or tgz .

To create a tar.gz archive from the given files, you can use the following command:

Tar -czf archive.tar.gz file1 file2

The -z option tells tar to compress the archive using the gzip algorithm.

Creating a Tar Bz2 archive

Another popular compression algorithm for tar files is bzip2. When compressing tar archives with bzip2, the archive name must end with either tar.bz2 or tbz .

When the -j option is specified, tar will use the bzip2 archive compression algorithm.

The following command will create a tar.bz2 archive from the given files:

Tar -cjf archive.tar.bz2 file1 file2

Tar archive listing

To list the contents of a tar archive, use the --list (-t) operation.

Tar -tf archive.tar file1 file2 file3

If you specify the contents of the archive using the --verbose (-v) option, tar will print more information, such as owner, file size, timestamp, etc.:

Tar -tvf archive.tar -rw-r--r-- linuxize/users 0 2018-09-08 20:15 file1 -rw-r--r-- linuxize/users 0 2018-09-08 20:15 file2 -rw-r--r-- linuxize/users 0 2018-09-08 20:15 file3

Extracting the Tar Archive

To extract a tar archive, use the --extract (-x) operator and specify the archive file name:

Tar -xf archive.tar

The -v option is also usually added to print the names of the extracted files.

Tar -xvf archive.tar

By default, tar will extract the contents of the archive in the current working directory. Use --directory (-C) to extract archived files in a specific directory:

For example, to extract the contents of an archive into the /opt/files directory, you could use:

Tar -xf archive.tar -C /opt/files

Extracting Tar Gz and Tar Bz2 archives

When extracting compressed archives, such as tar.gz or tar.bz2, you do not need to specify a decompression option. The command is the same as when extracting the tar archive:

Tar -xf archive.tar.gz tar -xf archive.tar.bz2

Extracting specific files from a tar archive

To extract specific files from a tar archive, add a list of file names, separated by spaces, to be extracted after the archive name:

Tar -xf archive.tar file1 file2

When extracting files, you must provide their exact names, including the path, as typed --list (-t).

Extracting one or more directories from an archive is similar to extracting files:

Tar -xf archive.tar dir1 dir2

If you try to extract a file that does not exist, you will receive an error message similar to the following:

Tar -xf archive.tar README tar: README: Not found in archive tar: Exiting with failure status due to previous errors

Extracting files from a Tar archive using a wildcard

To extract files from an archive based on a wildcard pattern, use the --wildcards switch and specify the pattern to prevent shell interpretation.

For example, to extract files whose names end in .js ( Javascript files), you can use:

Tar -xf archive.tar --wildcards "*.js"

Adding files to an existing archive

To append files or directories to an existing tar archive, use the --append (-r) operation.

For example, to add a file named newfile to archive.tar, you could use:

Tar -rvf archive.tar newfile

Removing files from a tar archive

Use the --delete operation to delete files from the archive.

For example, to remove a file named file1 from archive.tar, you could use:

Tar --delete -f archive.tar file1

Conclusion

By now you should have a good understanding of how to create and extract tar archives.

tar- the most common archiver used on Linux systems. tar itself is not an archiver in the usual sense of the word, because it does not use compression itself. At the same time, many archivers (for example, Gzip or bzip2) do not know how to compress multiple files, but work only with one file or input stream. Therefore, most often these programs are used together. tar creates an uncompressed archive that contains selected files and directories, while preserving some of their attributes (such as permissions). After this, the resulting *.tar file is compressed with an archiver, for example, gzip. This is why archives usually have the extension .tar.gz or .tar.bz2 (for gzip and bzip2 archivers, respectively)

Usage

tar is launched with the obligatory indication of one of the main actions, the most common of which is creating and unpacking archives, then other parameters are set depending on the specific operation.

Creating an archive

To create an archive, you need to tell tar the appropriate action, which is done using the -c switch. Additionally, the -f switch is required to pack the contents into a file. Next, we will first indicate the name of the future archive, and then the files that we want to pack.

Tar -cf txt.tar *.txt

This command will pack all files with a txt extension into a txt.tar archive. This is how it is created simple archive without compression. To use compression, you don't need to run anything else; you just need to tell tar which archiver to use to compress the archive. For the two most popular archivers, gzip and bzip2, the keys will be -z and -j, respectively.

Tar -cvzf files.tar.gz ~/files

will pack the ~/files folder with all its contents into a compressed one using g zip archive.

Tar -cvjf files.tar.bz2 ~/files

will create a similar archive using bzip2 for compression.

The -v switch enables displaying a list of packed files during operation. Unfortunately, more advanced indication of the progress of the process (for example, indicating the degree of completion in percent) is not provided in tar. To do this, use graphical archivers (for example, Xarchiver) or use file manager tools.

In addition to gzip and bzip2, you can use, for example, lzma (switch –lzma) or xz (switch -J), and the corresponding archiver must be installed on the system.

Unpacking the archive

The “unpacking” action is specified using the -x switch. And here again you will need the -f switch to specify the name of the archive file. We will also add the -v switch to visually display the process progress.

Tar -xvf /path/to/archive.tar.bz2

will unpack the contents of the archive into the current folder. An alternative location for unpacking can be specified using the -C switch:

Tar -xvf archive.tar.bz2 -C /path/to/folder

Viewing archive contents

To view the contents of the archive, use the following command:

Tar -tf archive.tar.gz

It will display a simple list of files and directories in the archive. If you add the -v switch, a detailed list will be displayed indicating the size, access rights and other parameters (the same as with ls -l)

Other features

tar provides many useful features. For example, you can specify files and directories that will not be included in the archive, add files to an existing archive, take a list of objects to pack from a text file, and much more. As always, it will help you understand all the variety of options.

man tar tar --help