How to Compress Files in Linux Using the tar Command
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.tar
that contains the filefile1.txt
,file2.txt
, and all contents indirectory/
.Output:
Create a new compressed
tar
archive using Gzip.console$ tar -czvf archive.tar.gz file1.txt file2.txt directory/
The above command creates a new
archive.tar.gz
and 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.tar
file into your working directory.Output:
Extract files from a compressed
tar
archive.console$ tar -xzvf archive.tar.gz
The above command extracts all files from the compresses
archive.tar.gz
file 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.tar
file without extraction.Output:
Create a compressed
tar
archive using Bzip.console$ tar -cjvf archive.tar.bz2 file1.txt file2.txt directory/
The above command creates a new compressed
archive.tar.bz2
file using thebzip2
utility.Output:
Extract files from a Bzip
tar
archive.console$ tar -xjvf archive.tar.bz2
The above command extracts all files from the
archive.tar.bz2
to the your working directory.Output:
Create a new
tar
archive using thexzipped
compression format.console$ tar -cJvf archive.tar.xz file1.txt file2.txt directory/
The above command creates a new compressed
archive.tar.xz
file using xz.Output:
Extract files from an
xzipped
archive file.console$ tar -xJvf archive.tar.xz
The above command extracts files from the
archive.tar.xz
file 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
newfile
to the existingtar
archivearchive.tar
.Output:
Update files in an existing tar archive.
console$ tar -uvf archive.tar updatedfile
The above command updates the
updatedfile
file in thearchive.tar
archive if its newer than the existing version.Output:
Advanced Usage Scenarios
Exclude files and directories from
tar
archive.console$ tar --exclude='*.log' -cvf archive.tar directory/
The above command creates a new
archive.tar
file that includes all files except.log
files 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 toremotehost
using 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/file
fromarchive.tar
.Output:
Verify the integrity of a tar archive.
console$ tar -tvf archive.tar > /dev/null
The above command lists all contents of
archive.tar
and discards the output to verify the file integrity.Use
tar
withfind
to archive specific files.console$ find . -name "*.txt" -print0 | tar -cvf archive.tar --null -T -
The above command finds all
.txt
files and archives them to thearchive.tar
file.Output:
Extract files to a specific directory.
console$ tar -xvf archive.tar -C /path/to/directory/
The above command extracts files from
archive.tar
to/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 MB
for 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.