Published: July 18, 2026
Read: 12 min
In: Technology & Development

TL;DR

To effectively use the tar command in Linux, you can try these methods:

  1. Combine multiple files or directories into a single tar file with tar -cvf archive.tar /path/to/directory.
  2. Retrieve the contents of a tar file using tar -xvf archive.tar.
  3. View the files and directories within a tar file without extracting them by running tar -tvf archive.tar.
  4. Create compressed archive files using tar -czvf archive-name.tar.gz /path/to/directory for gzip or tar -cjvf archive-name.tar.bz2 /path/to/directory for bzip2 compression.
  5. Extract specific files from an archive with tar -xzvf archive-name.tar.gz path/to/specific/file, saving time and space.
  6. Update an existing archive by adding new files using tar -rvf archive-name.tar new-file, avoiding the need to create a new archive.

Read the guide below to learn how to use the Tar command in Linux and best practices to use the Tar command.

Managing files on Linux can be a real challenge, especially with so many files and directories to handle. But don’t worry, the tar command is here to help. It lets you bundle, compress, and manage your files easily. In this post, I’ll show you 16 different ways to use the tar command, from basic tasks to advanced techniques like encryption and automation. By the end, you’ll have practical skills to make your file management simpler and more efficient. Let’s make your Linux experience smoother.

What is Tar Command in Linux?

The tar command in Linux is a powerful tool used for creating, maintaining, extracting, and managing tar files—often referred to as tarballs. The name tar stands for Tape Archive, reflecting its original purpose for writing data to tape drives. This command is commonly used for file compression and archiving, allowing users to bundle a collection of files and directories into a single file. The resulting archive can be optionally compressed using additional utilities like gzip or bzip2.

Here are some basic usages of the tar command:

  • Creating an archive: tar -cvf archive_name.tar directory_to_archive/
  • Extracting an archive: tar -xvf archive_name.tar
  • Viewing the contents of an archive: tar -tvf archive_name.tar
  • Creating a compressed archive: tar -czvf archive_name.tar.gz directory_to_archive/ (using gzip)
  • Extracting a compressed archive: tar -xzvf archive_name.tar.gz

These options and utilities make tar an essential command for file management and data transfer in Linux environments.

How to Use Tar Command in Linux?

To use the tar command in Linux, you start by creating an archive with tar -cvf archive_name.tar directory_name, where -c creates the archive, -v shows the process, and -f specifies the filename. To extract an archive, use tar -xvf archive_name.tar, where -x extracts the files.

For viewing contents, use tar -tvf archive_name.tar, and to compress, add -z for gzip (tar -czvf archive_name.tar.gz directory_name) or -j for bzip2 (tar -cjvf archive_name.tar.bz2 directory_name).

Keeping reading the article below to learn 16 different methods to use the tar command in Linux:

1. Creating a Basic Archive

Creating a basic archive is essential for grouping multiple files or directories into a single file, making it easier to store and transfer them.

  1. Access your command line interface by pressing Ctrl+Alt+T.
opening-terminal
  1. Change the current working directory to where your files are located using:

cd /path/to/directory

Replace /path/to/directory with the directory name you want to compress.

navigating to directory
  1. Run the tar command to create the archive:

tar -cvf archive.tar /path/to/directory
  • -c: Create a new archive.
  • -v: Verbosely list files processed.
  • -f: Specifies the archive file name.

This command creates a tar archive named archive.tar from the specified directory.

2. Extracting an Archive

Extracting an archive is necessary to retrieve the original files and directories from a tar archive.

  1. Access your command window and navigate to the directory.
  2. Run the tar command to extract the archive:

tar -xvf archive.tar
  • -x: Extract files from an archive.

This command extracts files from archive.tar.

3. Listing Contents of an Archive

Listing the contents of an archive allows you to see what files and directories are stored in the archive without extracting them.

  1. Launch your Terminal window and go to your desired directory.
  2. Execute the following command:

tar -tvf archive.tar

This command lists the contents of archive.tar without extracting.

4. Creating Compressed Archive Files

Compress files efficiently using tar with gzip or bzip2, reducing file size for easier storage and faster transmission. Essential for managing large datasets or backups. Follow these steps:

  1. Open your Terminal.
  1. Change the current working directory to where your files are located using.
  1. To create a gzip compressed archive, run the command: 

type tar -czvf archive-name.tar.gz /path/to/directory

If you are looking for quick compression then this method is best. 

  1. For the bzip2 compressed archive, use the command: 

tar -cjvf archive-name.tar.bz2 /path/to/directory

If you are looking for better compression, then this method is best.

  1. Confirm the creation of your compressed file by listing its details with the command: 

ls -lh archive-name.tar.*

This lists the details of the newly created archive, confirming its creation and size.

5. Extracting Specific Files from an Archive

Quickly extract specific files or directories from a tar archive without needing to decompress the entire archive, saving time and disk space in targeted retrievals. Here is the step-by-step guide:

  1. Launch the command line interface.
  2. Preview the contents of the archive by executing the command:

tar -tzvf archive-name.tar.gz 

Lists all files in the archive, helping you to identify specific file paths.

  1. To extract specific files, run the command:

tar -xzvf archive-name.tar.gz path/to/specific/file

Verify the files have been extracted to the current directory.

6. Updating Files in an Archive

Updating files in an archive allows you to add or replace files in an existing tar archive without recreating it from scratch.

  1. Launch your Terminal window and go to the directory.Type the following command and press enter:

tar -uvf archive.tar updatedfile.txt

This command updates updatedfile.txt in archive.tar if it is newer than the existing file.

7. Appending Files to an Existing Archive

Easily add new files to an existing tar archive. This feature is perfect for updating archive contents without creating a new archive from scratch.

  1. Access the command line and change to the directory where your archive is located.
  2. Add new files to the archive with: 

tar -rvf archive-name.tar new-file

Appends new files to the existing archive without recreating it.

  1. Confirm the addition by listing the archive’s contents by running the command: 

tar -tvf archive-name.tar

The output will be:

8. Excluding Files from an Archive

Excluding files or directories from an archive is useful when you want to archive a directory but omit certain files or subdirectories.

  1. Access your command window and navigate to the directory where the files are located.
  2. Run the following command to create the archive while excluding specific files:

tar --exclude=/home/guestuser/Downloads/file1.txt -cvf archive.tar /home/guestuser/Downloads

This command creates a tar archive while excluding the specified path.

9. Splitting an Archive into Multiple Files

Splitting a large archive into smaller parts is useful for easier storage, transfer, or handling when dealing with size limits on files.

  1. Open your Terminal and move to the desired directory. Type the following command and press Enter:

tar -cvf - /path/to/directory | split --bytes=500MB - archive.tar.part

This command creates a tar archive and splits it into multiple parts of 500MB each.

10. Preserving File Permissions and Ownership

Preserving file permissions and ownership is crucial when archiving system files or directories to ensure they retain their original properties when extracted.

  1. In your Terminal, run the following command to create the archive while preserving permissions and ownership:

tar -cvp --preserve-permissions --file=archive.tar /path/to/directory

This command creates a tar archive while preserving file permissions and ownership.

11. Comparing Archive Contents with the File System

Ensure data integrity by comparing the contents of a tar archive with the file system, identifying discrepancies and ensuring accurate backups or archives. Follow these steps to compare the archive content:

  1. Launch your command line tool.
  2. Compare the archive contents against the file system by typing the command: 

tar -dvf archive-name.tar
  1. Analyze the output for any discrepancies or missing files.

12. Creating Incremental Backups

Save disk space and backup time by creating incremental backups with tar, which only archives changes since the last backup, ideal for regular backup routines. Here is how to do it:

  1. Begin by accessing your command line interface.
  2. Create a full backup by using the following command: 

tar -czvf full-backup.tar.gz /path/to/directory

Replace the /path/to/directory with the directory name of which you want to create backup.

  1. For incremental backups, use the command: 

tar --listed-incremental=/path/to/snapshot.file -czvf incremental-backup.tar.gz /path/to/directory

Creates backups of changes since the last backup, saving space and time.

creating incremental backup of a directory

13. Securing Tar Archives with Encryption

Enhance the security of your tar archives by encrypting them. This method is crucial for protecting sensitive data in storage or transit. Here is the step-by-step guide:

  1. Access your command line tool.
  2. Encrypt your tar archive by running the command: 

gpg -c archive-name.tar.gz

Secures your archive by encrypting it with a password.

  1. To access the contents later, decrypt with the command: 

gpg archive-name.tar.gz.gpg

The encrypted archive will be decrypted.

14. Automating Tar Tasks with Scripts

Streamline repetitive tar operations by automating them with scripts. This approach increases efficiency and consistency in backup and archiving processes. Steps to automate tar tasks with scrips are:

  1. Use a text editor to write your tar command(s) in a .sh file.
  1. Change the script’s permissions to executable with:

chmod +x your-script.sh

Makes the script file executable, allowing it to run as a program.

  1. Execute the script by typing: 

./your-script.sh

Runs the script to automate the tar tasks you’ve defined.

15. Extracting Archives to Specific Directories

Directly extract tar archives to specific directories, enabling organized data management and restoration processes without cluttering the working directory. Follow these steps:

  1. Access the command line interface.
  2. To extract an archive to a specific directory, execute the command: 

tar -xzvf archive-name.tar.gz -C /target/directory

Replace target/directory with the name of the directory in which you want to extract the files.

  1. Check the specified directory to ensure the files were extracted correctly.

16. Using Tar with Pipes for Efficient Workflows

Leverage piping with tar for efficient data transfer between commands, minimizing disk usage and streamlining data backup and restoration workflows. Here is the guide:

  1. Launch the command line.
  2. Directly compress and backup a directory by running the command: