tar is a utility for packing and unpacking multiple files into one. Unlike traditional archivers, tar itself does not compress the resulting archive. In other words, all tar does is put a bunch of files into one, with a size no smaller than the sum of the originals.
As an addition for compressing or decompressing, tar can use the gzip utility, since it natively supports an interface with it.
Archiving
$ tar -cf /my/target/dir/file.tar myfile1 myfile2 mydir1 mydir2
The -c parameter tells tar to create an archive.
The -f parameter and the path that follows it is the path to the target file (in our case, the archive will be created with the filename file.tar and will be located at /my/target/dir/)
myfile1 myfile2 mydir1 mydir2 is simply a set of files and directories that need to be put into the archive. Directories are traversed recursively, and the archiver puts the files from the directories and all subdirectories into the archive file while preserving their relative layout.
Archiving with compression
$ tar -czf /my/target/fir/file.tgz myfile1 myfile2 mydir1 mydir2
Here the -z option has been added, which tells tar to use gzip to compress the resulting archive.
The rest of the options are the same as in plain archive creation.
Extracting all files
$ tar -xf myfile.tar
The -x parameter means the files need to be extracted.
The -f parameter specifies the tar file from which the files need to be extracted.
The files will be extracted to the current path.
Extracting specific files
$ tar -xf myfile.tar myfile1 myfile2
Here the parameters myfile1 and myfile2 have been added - these are the names of the files in the archive that need to be extracted.
Second example:
$ tar -xf myfile.tar myfile*
This example tells tar to extract all files whose name starts with myfile.
Extracting files from tar.gz or tgz (from compressed tar archives)
$ tar -xzf myfile.tgz
The -z parameter indicates that the archive should be processed using the gzip utility in order to first decompress it and only then extract the files.
Listing files in an archive
$ tar -tf myfile.tar
The -t parameter tells tar to show the list of files.
The -f parameter and the filename following it point to the archive to list files from.
Second example - let's show files filtered by a pattern:
$ tar -tf myfile.tar myfile*
As you can see, we asked to show only files whose names start with "myfile".
Adding files to an archive
$ tar -rf myfile.tar myfile3 myfile4
The -r parameter indicates that files need to be added to an already existing archive.
The -f parameter and the filename after it point to the archive file to work with.
The myfile3 and myfile4 parameters specify which files to add to the archive.
Adding only newer files
$ tar -uf myfile.tar myfile3 myfile4
Add to the archive only files that are newer than the ones already there (i.e., update the archive).
The -u parameter indicates that the archive files need to be updated.
The -f parameter points to the archive file to work with.
The myfile3 and myfile4 parameters are the files that need to be updated.
Merging archives
$ tar -Af myfile1.tar myfile2.tar
With this command we combine 2 tar archives into one.
The -A parameter indicates that the archives need to be concatenated.
The -f parameter points to the archive file to append to.
The myfile2.tar parameter is the archive to append.
Using bzip2 instead of gzipTo do this, replace the -z option with -j everywhere. Everything else stays the same.
Keep in mind that the bzip2 utility must already be installed on the system, which is often not the case, unlike gzip.
Comments