Skip to content

tar Linux Command Guide

The Linux tar command is a versatile tool used for archiving files and directories. It allows you to create compressed or uncompressed archives, extract files from an archive, and more. With tar, you can easily manage and backup large amounts of data while saving disk space. The command offers various options for customization, including specifying compression levels, adding files to an existing archive, and excluding specific files or directories. Whether you’re a beginner or an experienced user, mastering the tar command is essential for efficiently working with files and directories in a Linux environment.

tar Syntax:

Terminal window
tar [options] [file or directory]

Options:

OptionDescription
-cCreate a new archive
-xExtract files from an archive
-fSpecify the file name of the archive
-vVerbose mode
-zCompress the archive with gzip
-jCompress the archive with bzip2
-tList the contents of an archive
-aAuto-detect compression format

Parameters:

ParameterDescription
file or directorySpecifies the file or directory to be archived or extracted

tar Command Samples:

Create a Tarball

Terminal window
tar -cvf archive.tar file1 file2

Creates a tarball named “archive.tar” containing “file1” and “file2”.

Extract a Tarball

Terminal window
tar -xvf archive.tar

Extracts all files from the tarball “archive.tar”.

List Files in a Tarball

Terminal window
tar -tvf archive.tar

Lists all files included in the tarball “archive.tar”.

Compress with Gzip

Terminal window
tar -czvf archive.tar.gz file1 file2

Creates a tarball named “archive.tar.gz” with gzip compression.

Extract Gzipped Tarball

Terminal window
tar -xzvf archive.tar.gz

Extracts files from a gzipped tarball “archive.tar.gz”.

Create a Tarball with Compression

Terminal window
tar -cvzf archive.tar.gz folder

Creates a tarball “archive.tar.gz” of a folder with gzip compression.

Extract Specific Files from Tarball

Terminal window
tar -xvf archive.tar file1

Extracts only “file1” from the tarball “archive.tar”.

How do I create a tar archive of a directory in Linux?

To create a tar archive of a directory in Linux, use the following command:

Terminal window
tar -cvf archive.tar /path/to/directory

How do I extract a tar archive in a specific directory in Linux?

To extract a tar archive in a specific directory in Linux, execute the following command:

Terminal window
tar -xvf archive.tar -C /path/to/extract

How do I compress a directory using tar in Linux?

To compress a directory using tar in Linux, use the following command with gzip compression:

Terminal window
tar -zcvf archive.tar.gz /path/to/directory

How do I list the contents of a tar archive file in Linux?

To list the contents of a tar archive file in Linux, run the following command:

Terminal window
tar -tvf archive.tar

How do I extract a single file from a tar archive in Linux?

To extract a single file from a tar archive in Linux, use the following command:

Terminal window
tar -xvf archive.tar path/to/file

How do I update a tar archive with new files in Linux?

To update a tar archive with new files in Linux, use the following command:

Terminal window
tar -uvf archive.tar newfile.txt

Applications of the tar command

  • Creating a tar archive
  • Extracting files from a tar archive
  • Adding files to an existing tar archive
  • Compressing a tar archive using gzip
  • Extracting a compressed tar archive using gzip
  • Compressing a tar archive using bzip2
  • Extracting a compressed tar archive using bzip2
  • List the contents of a tar archive
  • Extracting specific files from a tar archive
  • Verbose output while creating or extracting a tar archive