Common Gnu Tar Commands Cheatsheet: Essential Guide for Users

The GNU tar command is a versatile utility for creating, managing, and extracting archives in Linux and other UNIX-like operating systems. This guide provides a comprehensive cheatsheet of common GNU tar commands, helping users to efficiently manipulate archives.

Creating Archives

Creating archives is one of the primary functions of the tar command. Here are some essential commands for creating different types of archives:

  • Create a Tar Archive

    tar -cf output.tar input/
    

    This command creates a tar archive named output.tar from the contents of the input/ directory.

  • Create a Gzip Compressed Tar Archive

    tar -czf output.tar.gz input/
    

    This command creates a tar archive named output.tar.gz and compresses it using the gzip algorithm.

  • Create a Bzip2 Compressed Tar Archive

tar -cjf output.tar.bz2 input/

This command creates a tar archive named output.tar.bz2 and compresses it using the bzip2 algorithm.

  • Create an XZ Compressed Tar Archive
    tar -cJf output.tar.xz input/
    

    This command creates a tar archive named output.tar.xz and compresses it using the xz algorithm.

Extracting Archives

Extracting archives is another crucial function of the tar command. Here are some commands for extracting archives:

  • Extract a Tar Archive

    tar -xf output.tar
    

    This command extracts the contents of the output.tar archive in the current working directory.

  • Extract a Gzip Compressed Tar Archive

    tar -xzf output.tar.gz
    

    This command extracts the contents of the output.tar.gz archive in the current working directory.

  • Extract a Bzip2 Compressed Tar Archive

tar -xjf output.tar.bz2

This command extracts the contents of the output.tar.bz2 archive in the current working directory.

  • Extract Without Overwriting Existing Files
    tar -xkf output.tar
    

    This command extracts the contents of the output.tar archive but does not replace any currently existing files in the directory.

Listing Archive Contents

Listing the contents of an archive can be useful for verifying what files are included:

  • List the Contents of a Tar Archive
    tar -tf output.tar
    

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

Updating and Appending Archives

You can update or append files to existing archives using the following commands:

  • Update an Archive

    tar -uf output.tar input/
    

    This command updates the output.tar archive with new files from the input/ directory, only if they are not in the archive and are newer than existing files.

  • Append Files to an Archive

    tar -rf output.tar new-input/
    

    This command appends the contents of the new-input/ directory to the end of the output.tar archive.

Advanced Options

GNU tar offers several advanced options that can enhance your archiving tasks:

  • Verbose Mode

    tar -cvf output.tar input/
    

    The -v option shows the progress of the tar command, listing the files as they are processed.

  • Checkpoint Action

    tar --checkpoint-action='cmd' -cf output.tar input/
    

    This option runs a specified command every 10 archive members, which can be useful for monitoring progress.

  • Preserve Permissions

tar --preserve-permissions -xf output.tar

This option ensures that the user and group permissions of the extracted files are preserved and not updated to the current user.

  • Strip Components
    tar --strip-components=3 -xf output.tar
    

    This option extracts files from the archive, stripping a specified number of leading directory components.

Syntax and Options

Understanding the syntax and available options is crucial for using tar effectively:

  • Traditional Style

    tar cfv output.tar input/
    

    This style clusters options together without dashes.

  • UNIX Short Option Style

    tar -cfv output.tar input/
    

    This style uses a single dash and clusters options.

  • GNU Long Option Style

tar --create --file output.tar --verbose input/

This style uses a double dash and descriptive option names.

Commonly Used Options

Command Role Description
--create -c Operation Creates a new archive.
--list -t Operation Lists an archive's contents.
--extract -x Operation Extracts one or more items from an archive.
--append -r Operation Appends files to an existing archive.
--concatenate -A Operation Appends archives to an existing archive.
--compare --diff -d Operation Compares archive members with files on the system.
--file=<archive> -f <archive> Option Specifies the file.
--verbose -v Option Shows the files being worked on while running.
--bzip2 -j Option Reads or writes compressed archives through bzip2 format.
--gzip -z Option Reads or writes compressed archives through gzip format.
--xz -J Option Reads or writes compressed archives through xz format.

These options and commands provide a solid foundation for managing archives with the GNU tar utility.

Leave a Reply

Your email address will not be published. Required fields are marked *