tar

tar

The tar command in Unix and Linux is a versatile utility used for creating and manipulating archive files. It stands for "tape archive," as it was originally used for archiving files onto magnetic tape. Today, tar is commonly used to package files and directories into a single file, often compressed, for easier storage, distribution, and backup.

Basic Usage

The basic syntax for the tar command is:

tar [options] [archive-file] [file(s) or directory(s)]
  • options: Optional command-line options to control the behavior of tar.

  • archive-file: The name of the archive file to create or manipulate.

  • file(s) or directory(s): The file(s) or directory(s) to include in the archive.

Examples

Creating an Archive

To create a new archive file:

tar -cvf archive.tar file1.txt file2.txt directory/
  • -c: Create a new archive.

  • -v: Verbose mode (show progress).

  • -f archive.tar: Specify the archive file name (archive.tar in this case).

  • file1.txt file2.txt directory/: List of files and directories to include in the archive.

This command creates a new archive named archive.tar containing file1.txt, file2.txt, and the contents of directory/.

Extracting from an Archive

To extract files from an archive:

  • -x: Extract files from an archive.

  • -v: Verbose mode (show progress).

  • -f archive.tar: Specify the archive file name (archive.tar in this case).

This command extracts the contents of archive.tar to the current directory.

Compressing an Archive

To compress an archive using gzip:

  • -z: Compress the archive using gzip.

  • -f archive.tar.gz: Specify the gzip-compressed archive file name (archive.tar.gz in this case).

  • -c: Create a new archive.

  • -v: Verbose mode (show progress).

This command creates a gzip-compressed archive named archive.tar.gz containing the contents of directory/.

Options

Compression Options

  • -z: Compress the archive using gzip.

  • -j: Compress the archive using bzip2.

  • -J: Compress the archive using xz.

Archive Manipulation

  • -c: Create a new archive.

  • -x: Extract files from an archive.

  • -t: List the contents of an archive.

  • -r: Append files to an archive.

File and Directory Options

  • -f archive-file: Specify the archive file name.

  • -v: Verbose mode (show progress).

  • -C directory: Change to the specified directory before performing operations.

Practical Use Cases

Backup and Restore

To create a backup of important files and directories:

This command creates a compressed backup (backup.tar.gz) of the documents directory.

Distributing Software Packages

To package software files for distribution:

This command creates a compressed archive (software.tar.gz) of the software/ directory for distribution.

Archiving and Compression

To archive and compress multiple directories into one file:

This command creates a gzip-compressed archive (archive.tar.gz) containing directory1/ and directory2/.

Summary

The tar command is essential for creating, manipulating, and extracting archive files in Unix and Linux systems. Its flexibility with options allows for various operations such as creating, extracting, compressing, appending files, and more. Understanding these options and their applications can greatly enhance your ability to manage and distribute files efficiently.

help

Last updated