
Introduction
The tar command in Linux enables archiving and compression of files and directories. It's also known as tape archive (tar) and combines multiple files into a single archive file with the .tar extension that can be compressed to save disk space to simplify file transfers and backups.
This article explains how to use the tar command in Linux to perform archiving and file compression tasks.
tar Command Syntax
Below is the basic tar command syntax:
$ tar [options] [archive-file] [file/directory]
Within the above command, [options] modifies the tar command behavior, [archive-file] specifies the archive filename, and [file/directory] specifies the files or directories to include in the archive.
tar Command Options
| Option | Description |
|---|---|
-c |
Create a new archive. |
-x |
Extract files from an archive. |
-v |
Verbose mode. Display progress in the terminal. |
-f |
Filename. Specify the archive filename. |
-z |
Compress the archive using gzip. |
-j |
Compress the archive using bzip2. |
-J |
Compress the archive using xz. |
-t |
List the contents of an archive. |
-r |
Append files to an existing archive. |
-u |
Update files in an existing archive. |
Practical Examples of the tar Command
Create a new tar archive.
console$ tar -cvf archive.tar file1.txt file2.txt directory/
The above command creates a new
archive.tarthat contains the filefile1.txt,file2.txt, and all contents indirectory/.Output:

Create a new compressed
tararchive using Gzip.console$ tar -czvf archive.tar.gz file1.txt file2.txt directory/
The above command creates a new
archive.tar.gzand enables compression using Gzip.Output:

Extract files from a tar archive.
console$ tar -xvf archive.tar
The above command extracts all files from the
archive.tarfile into your working directory.Output:

Extract files from a compressed
tararchive.console$ tar -xzvf archive.tar.gz
The above command extracts all files from the compresses
archive.tar.gzfile to the working directory.Output:

List all contents in a tar archive.
console$ tar -tvf archive.tar
The above command lists all contents in the
archive.tarfile without extraction.Output:

Create a compressed
tararchive using Bzip.console$ tar -cjvf archive.tar.bz2 file1.txt file2.txt directory/
The above command creates a new compressed
archive.tar.bz2file using thebzip2utility.Output:

Extract files from a Bzip
tararchive.console$ tar -xjvf archive.tar.bz2
The above command extracts all files from the
archive.tar.bz2to the your working directory.Output:

Create a new
tararchive using thexzippedcompression format.console$ tar -cJvf archive.tar.xz file1.txt file2.txt directory/
The above command creates a new compressed
archive.tar.xzfile using xz.Output:

Extract files from an
xzippedarchive file.console$ tar -xJvf archive.tar.xz
The above command extracts files from the
archive.tar.xzfile to your working directory.Output:

Add new files to an existing tar archive.
console$ tar -rvf archive.tar newfile
The above command appends the file
newfileto the existingtararchivearchive.tar.Output:

Update files in an existing tar archive.
console$ tar -uvf archive.tar updatedfile
The above command updates the
updatedfilefile in thearchive.tararchive if its newer than the existing version.Output:

Advanced Usage Scenarios
Exclude files and directories from
tararchive.console$ tar --exclude='*.log' -cvf archive.tar directory/
The above command creates a new
archive.tarfile that includes all files except.logfiles indirectory/.Output:

Compress and archive files to a remote location.
console$ tar -czvf - directory/ | ssh user@remotehost "cat > /path/to/destination/archive.tar.gz"
The above command compresses
directory/and transfers it toremotehostusing SSH.Output:
vultr_user@vultr:~$ tar -czvf - /home/vultr_user/dir1 | ssh vultr_user@123.84.123.123 "cat > /home/vultr_user/dir2/archive.tar.gz" tar: Removing leading `/' from member names /home/vultr_user/dir1/ /home/vultr_user/dir1/file2.txt /home/vultr_user/dir1/file1.txt /home/vultr_user/dir1/file3.txt The authenticity of host '123.84.123.123 (123.84.123.123)' can't be established. ED25519 key fingerprint is SHA256:kfJi2tl589s4r3mdDLq32Ziksl1j5f2sXM6aUw. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes vultr_user@139.84.176.145's password: vultr_user@vultr:~$Extract a specific file from a tar archive.
console$ tar -xvf archive.tar path/to/file
The above command extracts
path/to/filefromarchive.tar.Output:

Verify the integrity of a tar archive.
console$ tar -tvf archive.tar > /dev/null
The above command lists all contents of
archive.tarand discards the output to verify the file integrity.Use
tarwithfindto archive specific files.console$ find . -name "*.txt" -print0 | tar -cvf archive.tar --null -T -
The above command finds all
.txtfiles and archives them to thearchive.tarfile.Output:

Extract files to a specific directory.
console$ tar -xvf archive.tar -C /path/to/directory/
The above command extracts files from
archive.tarto/path/to/directory/.Output:

Create a new tar archive split into multiple volumes
console$ tar -cvf - directory/ | split -b 5M - archive_part_
The above command creates a new tar archive using files from
directory/and splits it into multiple volumes of5 MBfor each file.Output:

Conclusion
You have used the tar command to archive and compress files, manage backups, and optimize storage on your system. For more command options, run the man tar command to view the tar manual page.