How to use the tar archiver

The tar program is   used to create a file system archive and unpack it. That is, it combines files and directories into one archive. For what it is needed, it is necessary to further compress these files. Since tar itself   does not know how to compress, other specialized programs such as gzip ,  bzip2 are used to compress the archive  . Since these programs compress only one file, using tar, we combine all the files we need into one, and use compression.

Create a .tar archive  .

Let’s consider an example of creating an archive. When creating an archive, the extension .tar is appended to the name.

tar -cf archive. tar file1 file2

With this command, we will create an archive named  archive.tar  from two files  file1  and  file2 .

Keys take the most important role. By prescribing the required keys, the necessary manipulations with the archive are performed. You can use these keys in short or in full.

The table shows the options for using the first key, as well as its short and complete spelling.

Short spelling of the key. Full spelling of the key. Description of key actions.
-A –Catenate, –concatenate add tar files to archive
-c –Create creates a new archive
-d –Diff, –compare find differences between archive and filesystem
  –Delete removes from archive
-r –Append add files to the end of the archive
-t –List list of archive contents
-u –Update only add files newer than the archived copy
-x –Extract, –get extract files from archive

This table shows several options for other keys used after the first key.

Short spelling of the key. Full spelling of the key. Description of key actions.
-f –File using an archive file
-C –Directory DIR use directory DIR
-v –Verbose detailed list of processed files
-n –Seek archive necessary for moving
-s –Preserve-order, –same-order sort the retrieved names in the same order as in the archive

 

There is a very long list of keys of this kind. To familiarize yourself with a complete list of keys and their functionality, you can use the command.

man tar

In the short spelling of keys, the prefix is used  if they are written through a dash. When writing keys together, this prefix is ​​placed once before the very beginning of the keys. It is also possible not to use a prefix at all. In the case of full spelling, the prefix – is added to the key  .

The following examples show different use cases.

tar -cf archive. tar file1 file2

tar cf archive. tar file1 file2

tar -c -f archive. tar file1 file2

tar –create –file = archive. tar file1 file2

All these commands are identical, they create  archive.tar , from files  file1  and  file2 .

Unpacking a tar archive

tar -xf archive. tar

Adding  file3  to the end of  archive.tar

tar -rf archive. tar file3

Listing the contents of a tar archive

tar -tf archive. tar

 

by Abdullah Sam
I’m a teacher, researcher and writer. I write about study subjects to improve the learning of college and university students. I write top Quality study notes Mostly, Tech, Games, Education, And Solutions/Tips and Tricks. I am a person who helps students to acquire knowledge, competence or virtue.

Leave a Comment