Useful Tips

Inside IT


Creation is performed with the key c. The syntax is as follows:

tar -czvf archive.tar.gz / home / dmosk

* in this example, an archive will be created archive.tar.gz user home directory (/ home / dmosk)
** where z - compress the archive in gzip (without this parameter, tar does not compress, but creates the so-called tarball), c - key to create an archive, v - verbose mode, that is, with the output to the screen of the process (very convenient for monitoring the progress of work, but you can skip the scripts), f - use a file (be sure to specify, since we work with files to a greater extent).

List and search for tar archive contents

If you want to simply list the contents of the tar archive rather than extracting them, you can use the “-t” option. The following command prints the contents of the tar archive.

Using the v option, you can print additional information about each file.

For tar.bz2 / bzip files use the “j” option.


Unpacking with a key x with the syntax:

tar -xvf archive.tar.gz

Unpacking the .gz file:

tar -xvf archive.tar.gz

* when an error occurs This does not look like a tar archive, you can use the command gzip -d archive.tar.gz.

tar -xvjf archive.tar.bz2

* key j is responsible for working with bz2.

If the system swears on bzip2, then you need to install it:

yum install bzip2

apt-get install bzip2

pkg install bzip2

* respectively, for CentOS (RPM based), Ubuntu (deb based), FreeBSD (BSD based).

If we see an error tar: Unrecognized archive format, use the following command:

bzip2 -d archive.tar.bz2

tar -xvzf archive.tar.gzip

* The z key is responsible for working with gzip.

Unpacked like gzip:

tar -xvzf archive.tgz

Create tar / tar.gz archive

Now that we’ve learned how to extract an existing tar archive, it's time to start creating new ones. The tar command can be used to send selected files to an archive or an entire directory. Here are some examples.

The following command creates a tar archive using directories, adding all the files in it and subdirectories

The above example does not create a compressed archive. A regular archive will be created that places several files at once without any real compression.

For compression, use the “z” or “j” option for gzip or bzip, respectively.

Unpacking without a subfolder

This method can be used for unpacking in a pre-prepared directory. There will be a certain effect of renaming the catalog or the “Unpack here” analogue:

tar -C / home / admin / mytar -xvf admin.tar.gz --strip-components 1

* catalog / home / admin / mytar must be created in advance, --strip-components 1 will skip one subfolder inside the archive.

Add files to compressed archives (tar.gz / tar.bz2)

We already said above that files cannot be added to compressed archives. However, you can still add files to compressed archives using one trick. Use the help of the gunzip command to unzip the archive, add the file to the archive, and compress it again.

For bzip files use bzip2 and bunzip2 commands respectively.

Commands for action

-AAdding files to the archive.
-cCreate a new archive file.
-dShow different data between the source directory and archive contents.
--deleteDelete files inside the archive.
-rAdd files to the end of the archive.
-tShow archive contents.
-uAdd files that are not in the archive.
-xExtract files from archive.

* You can not use several of the above keys in one command.

Additional options

--atime-preserveKeep the same timestamp for the file.
-b NSet the block size to N x 512.
-CChange directory. By default, the one we are in is used.
--checkpointShow folder names when reading archive file.
-GUse the old incremental backup format when displaying or retrieving.
-gUsing the new incremental backup format when displaying or retrieving.
-hDo not duplicate symbolic links. Only files pointed to by these symlinks.
-iIgnore blocks of zeros.
-jUsing bzip2.
--ignore-failed-readIgnore unreadable files.
-kWhen unpacking, existing files are not replaced by the corresponding files from the archive.
-L NChange of magnetic tape after N * 1024 bytes.
-mWhen retrieving, ignore object modification time.
-MMulti-volume archives.
-N DATESave only newer files relative to DATE
-OThe direction of the extract to standard output.
-pExtract secure information.
-PDoes not drop the leading slash (/) from the names.
-sSort files upon extraction.
--preserveSimilarly -ps
--remove-filesDelete source files after adding to archive.
--same-ownerSave owner when retrieving.
--totalsOutput bytes when creating the archive.
-vLogging of actions - displays a list of objects over which the action takes place.
-V NAMECreate an archive on a volume labeled NAME.
--versionShow tar version.
-wRequire confirmation for each action.
-WChecking the archive after recording.
--exclude FILEExclude FILE file.
-X FILEExclude FILE files.
-ZFilters the archive using compress.
-zUsing gzip.

* the current list of options can be obtained with the command man tar.

Tar does not work with zip archives. On UNIX systems, we use the zip and unzip utilities for this. To get started, put the necessary packages:

yum install zip unzip

apt-get install zip unzip

pkg install zip unzip

* respectively, for RPM based, deb based, BSD based.

zip -r / home / dmosk

* creates a catalog archive / home / dmosk to file

On a Windows-based system, only ZIP archives can be unpacked by built-in tools. To work with different archives, it is recommended to install an archiver, for example 7-Zip.

View Content tar, tar.gz, tar.bz2

For example, it goes without saying that it is not practical to unpack the entire huge archive if you just need to extract only one specific file or folder.

Of course, this can be done on Linux using the tar command, but first you will probably need to view the contents of the archive without unpacking it.

Show contents of tar file:

Show contents of tar.gz file:

Show contents of tar.bz2 file:

-tShow archive contents

Useful advice: There is no more need to remember all these -xvf, -xvzf, -xvif keys! This wonderful bash function allows you to unpack any type of archive with a single extract! Read more →