tar

Overview

The tar command is used to create, maintain, modify, and extract files that are archived in the tar format. It’s commonly used for backing up files or creating distributions.

Syntax

tar [options] [archive-file] [file or directory to archive]

Common Options

Option Description
-c Create a new archive
-x Extract files from an archive
-f Use archive file
-v Verbosely list files processed
-z Filter the archive through gzip
-j Filter the archive through bzip2
-t List the contents of an archive
-r Append files to the end of an archive
-u Only append files that are newer than copy in archive

Key Use Cases

  1. Creating backups of files and directories
  2. Distributing collections of files and directories
  3. Archiving old data
  4. Creating software distributions

Examples with Explanations

Example 1: Create a tar archive

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

Creates a new archive named ‘archive.tar’ containing all files in /path/to/directory

Example 2: Create a compressed tar archive (tarball)

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

Creates a gzip-compressed tar archive

Example 3: Extract files from an archive

tar -xvf archive.tar

Extracts all files from archive.tar to the current directory

Understanding Output

When using the verbose option (-v): - Each line shows a file being processed - Format: permissions owner/group size date time filename

Common Usage Patterns

  1. Creating compressed archives:

    tar -czvf archive.tar.gz files/
  2. Extracting compressed archives:

    tar -xzvf archive.tar.gz
  3. Listing contents:

    tar -tvf archive.tar

Performance Analysis

  • Use -z for better compression but slower processing
  • Use multiple cores with --use-compress-program=pigz
  • Avoid compressing already compressed files (like .jpg, .mp3)

Additional Resources