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
|-A||Adding files to the archive.|
|-c||Create a new archive file.|
|-d||Show different data between the source directory and archive contents.|
|--delete||Delete files inside the archive.|
|-r||Add files to the end of the archive.|
|-t||Show archive contents.|
|-u||Add files that are not in the archive.|
|-x||Extract files from archive.|
* You can not use several of the above keys in one command.
|--atime-preserve||Keep the same timestamp for the file.|
|-b N||Set the block size to N x 512.|
|-C||Change directory. By default, the one we are in is used.|
|--checkpoint||Show folder names when reading archive file.|
|-G||Use the old incremental backup format when displaying or retrieving.|
|-g||Using the new incremental backup format when displaying or retrieving.|
|-h||Do not duplicate symbolic links. Only files pointed to by these symlinks.|
|-i||Ignore blocks of zeros.|
|--ignore-failed-read||Ignore unreadable files.|
|-k||When unpacking, existing files are not replaced by the corresponding files from the archive.|
|-L N||Change of magnetic tape after N * 1024 bytes.|
|-m||When retrieving, ignore object modification time.|
|-N DATE||Save only newer files relative to DATE|
|-O||The direction of the extract to standard output.|
|-p||Extract secure information.|
|-P||Does not drop the leading slash (/) from the names.|
|-s||Sort files upon extraction.|
|--remove-files||Delete source files after adding to archive.|
|--same-owner||Save owner when retrieving.|
|--totals||Output bytes when creating the archive.|
|-v||Logging of actions - displays a list of objects over which the action takes place.|
|-V NAME||Create an archive on a volume labeled NAME.|
|--version||Show tar version.|
|-w||Require confirmation for each action.|
|-W||Checking the archive after recording.|
|--exclude FILE||Exclude FILE file.|
|-X FILE||Exclude FILE files.|
|-Z||Filters the archive using compress.|
* 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 archive.zip / home / dmosk
* creates a catalog archive / home / dmosk to file archive.zip.
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:
|-t||Show 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 →