Linux often uses two particular utilities to help manage files. The first utility is tape archiver, or tar. The second is a compression utility such as gzip. The purpose of tar is to bundle together multiple files into a single tarball with a .tar extension. This makes functions like downloads much easier, since there is only one download necessary to acquire multiple files. The server administrator creates the bundle of files, and whoever downloads the bundle extracts the files from it.
It is essential to know how to work with tar because a great deal of the software available for Linux is distributed in tarballs. The following is an example of creating a tarball:
# tar -cvf tarball.tar file1 file2 file3
This bundles file1, file2, and file3 into a tarball named tarball.tar.
tar Command Options
|-c||Create the tarball|
|-x||Extract the tarball.|
|-v||Enable verbose mode.|
|-r||Append more files to an existing tarball.|
|-t||Test the tarball or see what files are included in the tarball.|
|-f||Specify the name of the tarball in the next argument (must be used as the last option).|
tar Command Examples
1. [c]reate an archive and write it to a [f]ile:
# tar cf target.tar file1 file2 file3
2. [c]reate a g[z]ipped archive and write it to a [f]ile:
# tar czf target.tar.gz file1 file2 file3
3. [c]reate a g[z]ipped archive from a directory using relative paths:
# tar czf target.tar.gz --directory=path/to/directory .
4. E[x]tract a (compressed) archive [f]ile into the current directory [v]erbosely:
# tar xvf source.tar[.gz|.bz2|.xz]
5. E[x]tract a (compressed) archive [f]ile into the target directory:
# tar xf source.tar[.gz|.bz2|.xz] --directory=directory
6. [c]reate a compressed archive and write it to a [f]ile, using [a]rchive suffix to determine the compression program:
# tar caf target.tar.xz file1 file2 file3
7. Lis[t] the contents of a tar [f]ile [v]erbosely:
# tar tvf source.tar
8. E[x]tract files matching a pattern from an archive [f]ile:
# tar xf source.tar --wildcards "*.html"