Cheatsheet & Examples: tar
tar is a command-line utility for creating, extracting, and managing archive files, commonly used for packaging and compressing files in Unix-like systems. It supports various compression methods and file operations.
Create a tar archive
Example Usage:tar -cvf archive.tar file1 file2
What it does:
Creates a non-compressed tar archive named archive.tar containing file1 and file2.
Command-line Arguments Explained:
-c: Creates a new archive.-v: Enables verbose mode to display progress.-f: Specifies the archive filename (followed by the name, e.g.,archive.tar).
Extract a tar archive
Example Usage:tar -xvf archive.tar
What it does:
Extracts the contents of archive.tar to the current directory.
Command-line Arguments Explained:
-x: Extracts files from the archive.-v: Displays the extracted files.-f: Specifies the archive filename.
Create a compressed tar archive (gzip)
Example Usage:tar -czvf archive.tar.gz file1 file2
What it does:
Creates a gzip-compressed tar archive named archive.tar.gz with file1 and file2.
Command-line Arguments Explained:
-c: Creates a new archive.-z: Compresses the archive using gzip.-v: Enables verbose mode.-f: Specifies the archive filename.
Extract a compressed tar archive (gzip)
Example Usage:tar -xzvf archive.tar.gz
What it does:
Extracts the contents of a gzip-compressed tar archive (archive.tar.gz) to the current directory.
Command-line Arguments Explained:
-x: Extracts files from the archive.-z: Decompresses the archive using gzip.-v: Displays the extracted files.-f: Specifies the archive filename.
List contents of a tar archive
Example Usage:tar -tvf archive.tar
What it does:
Lists the files and directories in archive.tar without extracting them.
Command-line Arguments Explained:
-t: Lists the contents of the archive.-v: Displays detailed information (e.g., permissions, sizes).-f: Specifies the archive filename.
Append files to an existing tar archive
Example Usage:tar -rvf archive.tar file3
What it does:
Adds file3 to the end of archive.tar without overwriting existing content.
Command-line Arguments Explained:
-r: Appends files to the archive.-v: Displays the files being added.-f: Specifies the archive filename.
Update an existing tar archive
Example Usage:tar -uvf archive.tar file1
What it does:
Updates the archive with file1 if it has changed since the last addition.
Command-line Arguments Explained:
-u: Updates the archive by adding newer files.-v: Displays the update progress.-f: Specifies the archive filename.
Extract to a specific directory
Example Usage:tar -xvf archive.tar -C /path/to/directory
What it does:
Extracts the contents of archive.tar into the specified directory (/path/to/directory).
Command-line Arguments Explained:
-x: Extracts files from the archive.-v: Displays extracted files.-f: Specifies the archive filename.-C: Changes to the target directory before extraction.
Exclude specific files or directories
Example Usage:tar -cvf archive.tar --exclude='*.log' directory/
What it does:
Creates an archive of directory/ while excluding all .log files.
Command-line Arguments Explained:
-c: Creates a new archive.-v: Displays progress.-f: Specifies the archive filename.--exclude: Ignores files matching the specified pattern.
Extract specific files from an archive
Example Usage:tar -xvf archive.tar file1 file2
What it does:
Extracts only file1 and file2 from archive.tar.
Command-line Arguments Explained:
-x: Extracts files from the archive.-v: Displays the extracted files.-f: Specifies the archive filename.
Compress using bzip2
Example Usage:tar -cjvf archive.tar.bz2 file1
What it does:
Creates a bzip2-compressed tar archive (archive.tar.bz2) with file1.
Command-line Arguments Explained:
-c: Creates a new archive.-j: Compresses using bzip2.-v: Enables verbose mode.-f: Specifies the archive filename.
Decompress using xz
Example Usage:tar -xJvf archive.tar.xz
What it does:
Extracts a tar archive compressed with xz (archive.tar.xz).
Command-line Arguments Explained:
-x: Extracts files.-J: Decompresses using xz.-v: Displays extracted files.-f: Specifies the archive filename.
Preserve file permissions during extraction
Example Usage:tar -xvf archive.tar -p
What it does:
Extracts files while preserving their original permissions.
Command-line Arguments Explained:
-x: Extracts files.-v: Displays extracted files.-f: Specifies the archive filename.-p: Preserves file permissions (equivalent to--same-permissions).
Extract without following symbolic links
Example Usage:tar -xvf archive.tar -h
What it does:
Extracts files without resolving or following symbolic links.
Command-line Arguments Explained:
-x: Extracts files.-v: Displays extracted files.-f: Specifies the archive filename.-h: Follows symbolic links only if they point to files within the archive.
Use wildcard patterns to extract files
Example Usage:tar -xvf archive.tar --wildcards '*.txt'
What it does:
Extracts all .txt files from archive.tar using wildcard matching.
Command-line Arguments Explained:
-x: Extracts files.-v: Displays extracted files.-f: Specifies the archive filename.--wildcards: Enables wildcard pattern matching.
Create an archive with absolute paths
Example Usage:tar -cvf archive.tar -P /absolute/path/file
What it does:
Includes the full absolute path (/absolute/path/file) in the archive.
Command-line Arguments Explained:
-c: Creates a new archive.-v: Displays progress.-f: Specifies the archive filename.-P: Preserves absolute paths in the archive.
Create a tarball with GNU tar-specific options
Example Usage:tar --format=posix -cvf archive.tar file1
What it does:
Creates a tar archive using the POSIX format, ensuring compatibility with older systems.
Command-line Arguments Explained:
--format=posix: Uses the POSIX-standard tar format.-c: Creates a new archive.-v: Displays progress.-f: Specifies the archive filename.

