A flexible bash script wrapper for tar that simplifies archive creation and extraction with support for multiple compression formats, selective file handling, and skip lists.
- ποΈ Multiple Compression Formats: Support for GZIP, BZIP2, XZ, and LZIP
- π Selective Archiving: Include only specific files/directories using a package list
- π« Skip List: Exclude files/directories from archiving
- π Custom Working Directory: Specify source/destination directories
- β Validation: Comprehensive error checking and validation
- π¨ Colored Output: User-friendly colored console output
- π§ͺ Well-Tested: Comprehensive pytest test suite
- Clone or download this repository:
git clone https://github.com/JohnBlue-git/MyTarPackageScript.git
cd MyTarPackageScript- Make the script executable:
chmod +x my_tar.sh- (Optional) Add to your PATH for system-wide access:
sudo ln -s $(pwd)/my_tar.sh /usr/local/bin/my_tar./my_tar.sh [OPTIONS] <archive.tar>| Option | Description | Required | Default |
|---|---|---|---|
--mode=MODE |
Operation mode: create or extract |
β Yes | - |
--dir=PATH |
Working directory for source/destination | β No | Current directory |
--comp=TYPE |
Compression type: GZ, BZ2, XZ, or LZ |
β No | None |
--select=FILE |
Text file with list of files/dirs to include | β No | All files |
--skip=FILE |
Text file with list of files/dirs to exclude | β No | None |
--help |
Display help message | β No | - |
| Type | Extension | Description | Speed | Ratio |
|---|---|---|---|---|
GZ |
.tar.gz |
GZIP compression | Fast | Good |
BZ2 |
.tar.bz2 |
BZIP2 compression | Medium | Better |
XZ |
.tar.xz |
XZ compression | Slow | Best |
LZ |
.tar.lz |
LZIP compression | Medium | Very Good |
Create an uncompressed tar archive of the current directory:
./my_tar.sh --mode=create archive.tarCreate a GZIP compressed archive:
./my_tar.sh --mode=create --comp=GZ archive.tar.gz
# Output: archive.tar.gzCreate with BZIP2 compression:
./my_tar.sh --mode=create --comp=BZ2 archive.tar.bz2
# Output: archive.tar.bz2Extract an uncompressed archive:
./my_tar.sh --mode=extract archive.tarExtract a compressed archive:
./my_tar.sh --mode=extract archive.tar.gzExtract to a specific directory:
./my_tar.sh --dir=/path/to/destination --mode=extract archive.tar.gzCreate package_list.txt:
default
develop
release
default.xml
develop.xml
release.xml
Create archive with only selected files:
./my_tar.sh --mode=create --comp=GZ --select=package_list.txt archive.tar.gzCreate skip_list.txt:
non_existed.xml
non_existed
*.log
temp/
Create archive excluding specified files:
./my_tar.sh --mode=create --comp=GZ --skip=skip_list.txt archive.tar.gzArchive only selected files but exclude specific ones:
./my_tar.sh --mode=create --comp=XZ \
--select=package_list.txt \
--skip=skip_list.txt \
archive.tarArchive from a specific directory:
./my_tar.sh --dir=/path/to/source --mode=create --comp=GZ archive.tar.gzExtract to a specific directory:
./my_tar.sh --dir=/path/to/destination --mode=extract archive.tar.gz# Create a compressed archive from /data/project
# Include only files listed in package_list.txt
# Exclude files listed in skip_list.txt
./my_tar.sh \
--dir=/data/project \
--mode=create \
--comp=XZ \
--select=package_list.txt \
--skip=skip_list.txt \
backup_2026.tar.xzThe package list file (--select) contains file and directory names to include, one per line:
# Files
default.xml
develop.xml
release.xml
# Directories
default
develop
release
# Patterns (if your implementation supports wildcards)
*.conf
config/
The skip list file (--skip) contains file and directory names to exclude, one per line:
# Specific files
debug.log
temp.txt
# Directories
temp/
cache/
node_modules/
# Patterns
*.tmp
*.bak
.git/
- Lines starting with
#are treated as comments (if your script supports it) - Empty lines are ignored
- File/directory names are relative to the working directory
- Non-existent files in skip list are safely ignored
This project includes a comprehensive pytest test suite to ensure reliability.
- Install Python 3 (if not already installed)
- Install test dependencies:
pip install -r requirements-test.txtOr install manually:
pip install pytest pytest-cov pytest-xdistThe easiest way to run tests is using the provided test runner script:
# Make it executable first (one time only)
chmod +x run_tests.sh
# Run tests
./run_tests.shThis script will:
- β Check if pytest is installed
- π¦ Automatically install test dependencies if needed (from
requirements-test.txt) - π§ Make
my_tar.shexecutable if needed - π§ͺ Run all tests with verbose output
- β¨ Display colored, formatted results
- π Show pass/fail summary
Example output:
======================================
My Tar Package Script - Test Runner
======================================
π§ͺ Running tests...
tests/test_my_tar.py::TestMyTar::test_script_exists PASSED
tests/test_my_tar.py::TestMyTar::test_create_simple_archive PASSED
...
18 passed in 0.67s
β
All tests passed!
Run all tests:
pytestRun with verbose output:
pytest -vRun with coverage report:
pytest --cov=. --cov-report=html --cov-report=termRun specific test:
pytest tests/test_my_tar.py::TestMyTar::test_create_simple_archive -vRun tests in parallel (faster):
pytest -n autoThe test suite covers:
- β Archive creation (compressed and uncompressed)
- β Archive extraction (compressed and uncompressed)
- β All compression formats (GZ, BZ2, XZ, LZ)
- β
Selective file inclusion with
--select - β
File exclusion with
--skip - β Combined select and skip lists
- β
Custom working directories with
--dirparameter - β Error handling (invalid parameters, missing files)
- β Edge cases (empty lists, non-existent files)
Note: Tests use the existing ./files/ directory structure to ensure real-world functionality. The directory contains:
default/,develop/,release/directoriesdefault.xml,develop.xml,release.xmlfiles- Matches the structure defined in
package_list.txt
- Bash: Version 4.0 or higher
- tar: GNU tar (usually pre-installed on Linux)
- Compression Tools (optional, for compression support):
gzip- for GZ compressionbzip2- for BZ2 compressionxz- for XZ compressionlzip- for LZ compression
On Ubuntu/Debian:
sudo apt-get install gzip bzip2 xz-utils lzipOn CentOS/RHEL:
sudo yum install gzip bzip2 xz lzipOn macOS:
brew install gzip bzip2 xz lzip- Python 3.7 or higher
- pytest >= 7.0.0
- pytest-cov >= 4.0.0 (optional, for coverage)
- pytest-xdist >= 3.0.0 (optional, for parallel testing)
This section provides a quick reference for native tar commands. The my_tar.sh script wraps these commands to provide a simpler, more consistent interface.
# Create archive
tar -cpvf archive.tar file1 file2 directory/
# Extract archive
tar -xpvf archive.tarFlags:
-c= create archive-x= extract archive-p= preserve permissions-v= verbose output-f= specify filename
# Create compressed archive
tar -cpzvf archive.tar.gz file1 file2 directory/
# Extract compressed archive
tar -xpzvf archive.tar.gzAdditional flag: -z = gzip compression
# Create compressed archive
tar -cpjvf archive.tar.bz2 file1 file2 directory/
# Extract compressed archive
tar -xpjvf archive.tar.bz2Additional flag: -j = bzip2 compression
# Create compressed archive
tar -cpJvf archive.tar.xz file1 file2 directory/
# Extract compressed archive
tar -xpJvf archive.tar.xzAdditional flag: -J = xz compression
# Create compressed archive
tar -cpvf --lzip archive.tar.lz file1 file2 directory/
# Extract compressed archive
tar -xpvf --lzip archive.tar.lzAdditional flag: --lzip = lzip compression
Specify source and destination directories:
# Create archive from specific directory
tar -C /source/directory -cpvf backup.tar project/ report.txt
# Extract to specific directory
tar -C /output/directory -xpvf archive.tarAdditional flag: -C = change to directory
Include only files listed in a text file:
# Create archive with file list
tar -cpvf archive.tar -T package_list.txt --ignore-failed-readAdditional flags:
-T= read file list from file--ignore-failed-read= continue if files don't exist
Example package_list.txt:
default
develop
release
default.xml
develop.xml
release.xml
Exclude specific files or patterns:
# Exclude single file
tar -cpvf archive.tar --exclude='non_existed.xml' .
# Exclude multiple files
tar -cpvf archive.tar --exclude='non_existed.xml' --exclude='non_existed' .
# Exclude using brace expansion (bash)
tar -cpvf archive.tar --exclude={'non_existed.xml','non_existed'} .
# Exclude patterns
tar -cpvf archive.tar --exclude='*.log' --exclude='temp/*' .Additional flag: --exclude=PATTERN = exclude files matching pattern
The my_tar.sh script provides several advantages over native tar:
| Feature | Native Tar | my_tar.sh |
|---|---|---|
| Compression auto-detection | Manual flags | Automatic from extension |
| Skip list file | Multiple --exclude |
Single --skip=file.txt |
| Select list file | -T with specific syntax |
Simple --select=file.txt |
| Working directory | -C flag (can be confusing) |
Clear --dir=PATH |
| Error handling | Basic | Comprehensive validation |
| User feedback | Minimal | Colored, informative output |
Example comparison:
Native tar:
cd /source/directory
tar -cpzvf /output/archive.tar.gz -T package_list.txt --exclude='debug.log' .my_tar.sh:
./my_tar.sh --dir=/source/directory --mode=create --comp=GZ \
--select=package_list.txt --skip=skip_list.txt \
/output/archive.tar| Option | Description |
|---|---|
-c |
Create a new archive |
-x |
Extract files from archive |
-t |
List archive contents |
-v |
Verbose output |
-f FILE |
Use archive file |
-p |
Preserve permissions |
-z |
Gzip compression |
-j |
Bzip2 compression |
-J |
XZ compression |
--lzip |
Lzip compression |
-C DIR |
Change to directory |
-T FILE |
Get names to extract/create from file |
--exclude=PATTERN |
Exclude files matching pattern |
--ignore-failed-read |
Don't exit on unreadable files |
MyTarPackageScript/
βββ my_tar.sh # Main script
βββ package_list.txt # Example package list
βββ skip_list.txt # Example skip list
βββ README.md # This file
βββ requirements-test.txt # Test dependencies
βββ pytest.ini # Pytest configuration
βββ files/ # Example files directory
β βββ default.xml
β βββ develop.xml
β βββ release.xml
β βββ default/
β βββ develop/
β βββ release/
βββ tests/ # Test suite
βββ conftest.py # Pytest configuration
βββ test_my_tar.py # Main test file
- Built on top of GNU
tar - Inspired by common backup and archiving needs
- Test suite powered by pytest