tar - Linux


Overview

The tar command stands for tape archive, and is used primarily for creating and managing archives in Linux and Unix systems. The .tar file format bundles multiple files into a single file, facilitating easier distribution or backup. tar can also compress and decompress archives using various compression methods.

Syntax

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

Basic command structure:

  • Creating an archive: tar -cf archive_name.tar file1 file2
  • Extracting an archive: tar -xf archive_name.tar
  • Listing contents of an archive: tar -tf archive_name.tar

Options/Flags

  • -c : Create a new archive.
  • -x : Extract files from an archive.
  • -t : List the contents of an archive.
  • -f : Specify the filename of the archive. Always used with another option.
  • -v : Verbose output, shows progress in the terminal.
  • -z : Compress the archive using gzip.
  • -j : Compress the archive using bzip2.
  • -J : Compress the archive using xz.
  • -p : Preserve file permissions.
  • --exclude=<pattern> : Exclude files that match the pattern.
  • -r : Append files to the end of an existing archive.
  • -u : Only append files newer than the copy in the archive.
  • -W : Verify an archive after writing it.

Examples

  1. Creating a gzipped tar archive:
    tar -czvf archive_name.tar.gz /path/to/directory
    
  2. Extracting a gzipped tar archive:
    tar -xzvf archive_name.tar.gz
    
  3. Viewing the contents of tar archive:
    tar -tvf archive_name.tar
    
  4. Excluding files when creating an archive:
    tar -czvf archive_name.tar.gz --exclude='*.txt' /path/to/directory
    

Common Issues

  • File Permissions: When extracting files, users can sometimes face permissions issues. Use the -p flag to preserve file permissions.
  • Large Archive Management: Handling large archives without checking contents can lead to high resource consumption. Always list contents before extracting.
  • Incorrect Path: Ensure the paths are correct when creating or extracting archives to avoid errors.

Integration

The tar command can be integrated with ssh for transferring archives between servers:

tar zcvf - /path/to/local/dir | ssh user@remote.server "cat > /path/remote/dir/backup.tar.gz"

Another common integration is with cron to schedule regular backups:

0 2 * * * tar zcvf /path/to/backup/dir/backup_$(date +\%Y\%m\%d).tar.gz /path/to/dir
  • gzip, bzip2, xz: Commands for compressing files and streams.
  • cpio: Similar to tar, used for creating archives and extracting files.
  • rsync: Useful for synchronized backup because it can handle incremental file transfers.

For more detailed documentation, refer to the official GNU tar manual: GNU tar manual.