The //**tar**// command in Linux and Unix-like systems is used to create, extract, and manipulate archive files. A tar archive is a single file that contains multiple files and directories, similar to a [[unix_commands:zip|zip]] archive. The tar file format is commonly used for software distribution and backups.
Here are a few examples of how the tar command can be used:
- Create a tar archive: **tar -cvf archive.tar file1 file2 file3**
- Extract a tar archive: **tar -xf archive.tar**
- Extract a specific file or directory: **tar -xf archive.tar file1**
- Create a compressed (gzip) tar archive: **tar -zcvf archive.tar.gz file1 file2 file3**
- Extract a compressed (gzip) tar archive: **tar -zxvf archive.tar.gz**
- Create a compressed (bzip2) tar archive: **tar -jcvf archive.tar.bz2 file1 file2 file3**
- Extract a compressed (bzip2) tar archive: **tar -jxvf archive.tar.bz2**
* The **-c** option is used to create an archive
* The **-x** option is used to extract an archive
* The **-v** option is used to display verbose output/
* The **-f** option is used to specify the archive file name
* The **-z** option is used to create or extract a gzip compressed archive
* The **-j** option is used to create or extract a bzip2 compressed archive,
* The **-p** option is used to preserve the file permissions and ownership when creating an archive,
* The **-C** option is used to extract the archive to a specific directory.
It's also possible to use tar command to add files to an existing archive by using the **-r** option, for example:
tar -rvf archive.tar file4
This command will add **file4** to the **archive.tar**
You can also use **--exclude** option to exclude certain files or directories while creating an archive, for example:
tar -cvf archive.tar --exclude='*.log' /var/log
This command will create an archive of all files and directories in /var/log except for files with //.log// extension.