How to Compress Files in Linux Using the tar Command

Updated on August 16, 2024
 How to Compress Files in Linux Using the tar Command header image

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:

console
$ 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

  1. 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 file file1.txt, file2.txt, and all contents in directory/.

    Output:

    Creating a tar archive

  2. 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:

    Creating a gzipped tar archive

  3. 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:

    Extracting a tar archive

  4. 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:

    Extracting a gzipped tar archive

  5. 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:

    Listing contents of a tar archive

  6. 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 the bzip2 utility.

    Output:

    Creating a bzipped tar archive

  7. 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:

    Extracting a bzipped tar archive

  8. Create a new tar archive using the xzipped 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:

    Creating an xzipped tar archive

  9. 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:

    Extracting an xzipped tar archive

  10. Add new files to an existing tar archive.

    console
    $ tar -rvf archive.tar newfile
    

    The above command appends the file newfile to the existing tar archive archive.tar.

    Output:

    Appending files to an archive

  11. Update files in an existing tar archive.

    console
    $ tar -uvf archive.tar updatedfile
    

    The above command updates the updatedfile file in the archive.tar archive if its newer than the existing version.

    Output:

    Updating files in an archive

Advanced Usage Scenarios

  1. 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 in directory/.

    Output:

    Excluding files in an archive

  2. 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 to remotehost 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:~$ 
  3. Extract a specific file from a tar archive.

    console
    $ tar -xvf archive.tar path/to/file
    

    The above command extracts path/to/file from archive.tar.

    Output:

    Extracting a specific file

  4. 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.

  5. Use tar with find 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 the archive.tar file.

    Output:

    Using tar with find

  6. 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:

    Extracting to a specific directory

  7. 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 of 5 MB for each file.

    Output:

    Creating split archives

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.