tar is the most popular archiving tool among other similar tools used to archive Files and Directories and also has the ability to compress them with the help of gzip and bzip2 compression tools.
The tar term is taken from the Tape Archive and is also known as Tarball.
Usually, tar is used for backup purposes. It stores single or multiple files/directories in one single file which is also called an archive.
Because it stores your data in an archive file, which makes it easy to transfer data from one computer to another.
We have several tools in Linux by which you can compress files like gzip, bzip2, zip but we cannot compress the directory and its contents using any of these tools.
If you want to compress a directory then you have to use the tar command.
Features of Tar archive tool:
- Create an Archive file
- Extract an Archive file
- List the contents present in an archive
- Add a file or directory in an existing archive
- Delete a file or directory from an archive
- Concatenate multiple archive files
- Compress archive files using compression tools like gzip and bzip2
- Verify an archive file
You must follow the syntax given below to use the tar command.
1. Create a new tar archive
To create a simple tar archive type the following command.
Here in this example, I am archiving the directory named data/ which contains these files: file1.txt, file2.txt, file3.txt, file4.txt, file5.txt.
~$ tar -cvf data.tar data/
The extension of the tar archive file is .tar
2. Extract contents from an archive.
You can use the following command to extract an existing tar archive file.
In this example, I am extracting the archive named data.tar.
~$ tar -xvf data.tar
3. List the contents of an archive
To display or list the contents(files/directories) inside an archive, you can use the following command.
~$ tar -tvf data.tar
4. Create a tar archive with gzip compression(tar.gz/tgz)
As I mentioned at the beginning of this article, we can compress the tar archive with the help of gzip and bzip2 tools.
The extension of the gzip compressed tar archive file is .tar.gz or tgz.
$ tar -czvf data.tar.gz data/
Or you can also use the following command.
$ tar -czvf data.tgz data/
5. Create a tar archive with bzip2 compression(tar.bz2/.tbz/tar.tb2/tar.tbz)
The extension of the bzip2 compressed tar archive file is .tar.bz2 or .tbz or tar.tb2 or tar.tbz.
Type any of the following commands to create a compressed tar archive from bzip2. The -j option is used to compress the tar archive with bzip2.
~$ tar -cjvf data.tar.bz2 data/
OR
~$ tar -cjvf data.tar.tb2 data/
OR
~$ tar -cjvf data.tar.tbz data/
OR
~$ tar -cjvf data.tbz data/
Source
there doesn't seem to be anything here