Bash TAR Archive

Master the tar command for archiving files and folders

๐Ÿ“š What is TAR?

TAR (Tape Archive) is a Unix utility that bundles multiple files and directories into a single archive file. It preserves file permissions, ownership, and directory structures, making it ideal for backups and file distribution.


# Create a tar archive
tar -cf archive.tar files/
                                    

Output:

archive.tar created successfully

Key TAR Concepts

๐Ÿ“ฆ

Create

Bundle files into archive

tar -cf archive.tar files/
๐Ÿ“‚

Extract

Unpack tar archives

tar -xf archive.tar
๐Ÿ“‹

List

View archive contents

tar -tf archive.tar
๐Ÿ—œ๏ธ

Compress

Combine with compression

tar -czf archive.tar.gz files/

๐Ÿ”น Create TAR Archive

The tar -c command bundles multiple files and directories into a single archive file, preserving directory structures, permissions, timestamps, and ownership information intact. Combined with -f to specify the archive filename (tar -cf archive.tar folder/), this creates a comprehensive snapshot of your data organization. TAR (Tape ARchive) format maintains complete metadata fidelity, ensuring perfect restoration of original conditions during extraction. This functionality is essential for backups, software distribution, and data migration where maintaining structural integrity and file attributes is equally important as preserving content.

# Create archive from files
tar -cf archive.tar file1.txt file2.txt

# Create archive from directory
tar -cf backup.tar /home/user/documents/

# Create with verbose output
tar -cvf archive.tar folder/

Output:

folder/
folder/file1.txt
folder/file2.txt
folder/subfolder/

๐Ÿ”น Extract TAR Archive

Extracting TAR archives with tar -xf recreates the original file structure exactly as archived, restoring all contents to their precise original state including permissions and timestamps. The command tar -xf archive.tar extracts everything to the current directory, while tar -xf archive.tar -C /target/directory specifies an alternative destination. This extraction process maintains complete fidelity with the archived version, ensuring perfect reconstruction of complex directory hierarchies, symbolic links, and file attributes. Verification using tar -tf before extraction confirms contents and prevents potential overwriting conflicts.

# Extract to current directory
tar -xf archive.tar

# Extract with verbose output
tar -xvf backup.tar

# Extract to specific directory
tar -xf archive.tar -C /destination/path/

Output:

folder/
folder/file1.txt
folder/file2.txt

๐Ÿ”น List Archive Contents

Previewing ZIP archive contents with unzip -l before extraction provides valuable information about stored files, their sizes, compression ratios, and modification dates. This non-destructive examination helps verify archive integrity, identify specific files for selective extraction, estimate storage requirements, and confirm archive purpose before committing to full extraction. The detailed listing includes original file sizes, compressed sizes, compression percentages, and timestamps, offering comprehensive insight into archive composition. This preview capability supports informed decision-making for archive management and extraction planning.

# List all files in archive
tar -tf archive.tar

# List with detailed information
tar -tvf archive.tar

# List specific file
tar -tf archive.tar | grep "filename"

Output:

drwxr-xr-x user/group 0 2024-10-10 14:30 folder/
-rw-r--r-- user/group 1024 2024-10-10 14:30 folder/file1.txt
-rw-r--r-- user/group 2048 2024-10-10 14:31 folder/file2.txt

๐Ÿ”น Compressed TAR Archives

Combining TAR's archiving capabilities with compression algorithms creates efficient, space-saving archive files that maintain structural integrity while minimizing storage requirements. Key options include -z for gzip compression (tar -czf archive.tar.gz folder/), -j for bzip2, and -J for xz compression. These compressed archives significantly reduce storage needs and accelerate transfer times while preserving all TAR metadata benefits. The compression occurs during archiving, creating optimized workflows that bundle and compress in a single operation rather than separate steps.

# Create gzip compressed archive
tar -czf archive.tar.gz folder/

# Create bzip2 compressed archive
tar -cjf archive.tar.bz2 folder/

# Create xz compressed archive
tar -cJf archive.tar.xz folder/

# Extract compressed archive (auto-detects)
tar -xf archive.tar.gz

Output:

folder/
folder/file1.txt
folder/file2.txt

๐Ÿ”น Extract Specific Files

You can selectively extract individual files from archives by specifying their exact names or using wildcard patterns for multiple matches. This targeted extraction saves time and storage space when you only need certain files from large archives. For example, unzip archive.zip "*.txt" extracts all text files, while unzip archive.zip document.pdf retrieves only the PDF file. This approach is invaluable for developers working with large dependency archives or content creators managing asset libraries.

# Extract single file
tar -xf archive.tar folder/file1.txt

# Extract multiple specific files
tar -xf archive.tar file1.txt file2.txt

# Extract entire subdirectory
tar -xf archive.tar folder/subfolder/

Output:

folder/file1.txt

๐Ÿ”น Append to Archive

The -r option adds new files to existing uncompressed TAR archives without recreating the entire archive, supporting incremental backup strategies and collection management. The command tar -rf archive.tar newfile.txt appends the specified file to the archive, maintaining existing contents while expanding the collection. This functionality only works with uncompressed TAR filesโ€”compressed archives require complete recreation to add content. Append operations preserve efficiency when regularly updating archive collections, though periodic complete archive verification ensures long-term integrity and organization.

# Append file to existing archive
tar -rf archive.tar newfile.txt

# Append directory to archive
tar -rf backup.tar newfolder/

# Append with verbose output
tar -rvf archive.tar additional.txt

Output:

newfile.txt

๐Ÿ”น Exclude Files from Archive

Selectively omit specific files or patterns during compression using the exclusion -x flag followed by target patterns to skip. This fine-grained control allows you to create clean archives without temporary files, logs, or system artifacts. The command zip archive.zip folder/ -x "*.tmp" "*.log" compresses the directory while excluding temporary and log files. This exclusion capability is particularly valuable for creating production-ready deployment packages and minimizing archive sizes by removing unnecessary content.

# Exclude specific file
tar -czf archive.tar.gz --exclude="*.log" folder/

# Exclude multiple patterns
tar -czf backup.tar.gz --exclude="*.tmp" --exclude="cache/*" project/

# Exclude directory
tar -czf archive.tar.gz --exclude="node_modules" myapp/

Output:

folder/
folder/file1.txt
folder/file2.txt

๐Ÿง  Test Your Knowledge

Which option creates a gzip compressed tar archive?