Learn various techniques to combine and compress multiple files or directories into a single file to reduce storage footprint or simplify sharing.
tar
, along with zip
, is one of the basic commands to combine multiple individual files into a single file (called a "tarball"). tar
requires at least one command line option. A typical usage would be:
$ tar -cf newArchiveName.tar file1 file2 file3
# or
$ tar -cf newArchiveName.tar /path/to/folder/
The -c
flag denotes creating an archive, and -f
denotes that the next argument given will be the archive name—in this case it means the name you would prefer for the resulting archive file.
To extract files from a tar, it's recommended to use:
$ tar -xvf existingArchiveName.tar
-x
is for extracting, -v
uses verbose mode which will print the name of each file as it is extracted from the archive.
tar
can also generate compressed tarballs which reduce the size of the resulting archive. This can be done with the -z
flag (which just calls gzip
on the resulting archive automatically, resulting in a .tar.gz
extension) or -j
(which uses bzip2
, creating a .tar.bz2
).
For example:
# gzip
$ tar -czvf newArchive.tar.gz file1 file2 file3
$ tar -xvzf newArchive.tar.gz
# bzip2
$ tar -czjf newArchive.tar.bz2 file1 file2 file3
$ tar -xvjf newArchive.tar.bz2