Skip to content

Basic Examples

Mikhail Deynekin edited this page Dec 23, 2025 · 1 revision

Basic Examples

Practical examples for common Clean BOM Senior tasks. Focus on the simplified syntax: ./clean-bom-senior.sh "[PATH]" --options and the workflow of remove BOMs and fix line endings.

Table of Contents

Core Principle

Clean BOM Senior is designed for simplicity:

./clean-bom-senior.sh [PATH] [OPTIONS]

Where:

  • [PATH] = File or directory to process
  • [OPTIONS] = --dry-run, --backup, --verbose, --fix-crlf, etc.

Basic Commands

Remove BOM from Single File

Process one file and remove UTF-8 BOM:

./clean-bom-senior.sh document.php

Remove BOM from Entire Directory

Process all files in current directory:

./clean-bom-senior.sh .

Preview Changes Without Modifying

Use dry-run mode to see what would change:

./clean-bom-senior.sh . --dry-run

Create Backups Before Changes

Automatically backup files before modification:

./clean-bom-senior.sh . --backup

Show Verbose Output

Display detailed processing information:

./clean-bom-senior.sh . --verbose

Display Help

View all available options:

./clean-bom-senior.sh --help

Single File Processing

Remove BOM from PHP File

Problem: PHP file with UTF-8 BOM causes "headers already sent" error

./clean-bom-senior.sh config.php --verbose

Output:

✓ config.php: BOM removed (UTF-8)

Remove BOM from JSON File

Problem: JSON files with BOM fail to parse

./clean-bom-senior.sh data.json

Remove BOM from Python File

Problem: Python interpreter rejects encoding declaration with BOM

./clean-bom-senior.sh script.py --verbose

Remove BOM and Fix Line Endings

Problem: File has both BOM and Windows (CRLF) line endings

./clean-bom-senior.sh config.ini --fix-crlf --backup

This removes BOM and converts CRLF → LF

Batch Processing

Process All PHP Files

Remove BOM from all .php files in directory:

./clean-bom-senior.sh . --file-type php --verbose

Process All Configuration Files

./clean-bom-senior.sh . --file-type ini
./clean-bom-senior.sh . --file-type conf
./clean-bom-senior.sh . --file-type yaml

Process with Exclusions

Remove BOM from all files except those in .git and node_modules:

./clean-bom-senior.sh . --exclude .git --exclude node_modules --backup

Process Nested Directories

Recursively process all subdirectories:

./clean-bom-senior.sh src/ --verbose

Processes: src/ and all subdirectories

Verbose Report

Get detailed statistics:

./clean-bom-senior.sh . --verbose --backup

Shows:

  • Files processed
  • BOM types detected
  • Line ending issues
  • Summary statistics

Common Use Cases

Use Case 1: Clean Project After Windows Export

Scenario: Files exported from Windows with BOMs and CRLF endings

# Step 1: Preview changes
./clean-bom-senior.sh . --dry-run --fix-crlf

# Step 2: Apply with backup
./clean-bom-senior.sh . --fix-crlf --backup --verbose

# Step 3: Restore if needed
./clean-bom-senior.sh . --restore

Use Case 2: Fix PHP Project

Scenario: PHP files have BOMs causing header errors

# Process all PHP files
./clean-bom-senior.sh . --file-type php --backup

# Verify with verbose output
./clean-bom-senior.sh . --file-type php --verbose

Use Case 3: Prepare for Git Commit

Scenario: Clean all text files before committing

# Dry run first
./clean-bom-senior.sh . --dry-run --verbose

# Create backups and clean
./clean-bom-senior.sh . --backup --fix-gitattributes

# Commit changes
git add .
git commit -m "Remove BOM and fix line endings"

Use Case 4: Clean Markdown Documentation

Scenario: Markdown files have BOM issues

./clean-bom-senior.sh docs/ --file-type md --backup

Use Case 5: Automate in CI/CD Pipeline

Scenario: Run as part of build process

#!/bin/bash
./clean-bom-senior.sh . --dry-run
if [ $? -eq 0 ]; then
  echo "✓ No BOMs found"
else
  echo "⚠ BOMs detected, cleaning..."
  ./clean-bom-senior.sh . --backup
fi

Use Case 6: Fix JSON Configuration Files

Scenario: JSON files fail parsing due to BOM

./clean-bom-senior.sh config/ --file-type json --verbose

Advanced Examples

Selective File Type Processing

Process multiple file types:

./clean-bom-senior.sh . --file-type php --backup
./clean-bom-senior.sh . --file-type json --backup
./clean-bom-senior.sh . --file-type yaml --backup

Combine Multiple Options

./clean-bom-senior.sh src/ \
  --file-type php \
  --exclude tests \
  --exclude vendor \
  --fix-crlf \
  --backup \
  --verbose

This:

  • Targets PHP files in src/
  • Excludes tests/ and vendor/
  • Fixes CRLF line endings
  • Creates backups
  • Shows detailed output

Dry-Run Before Production

Best practice for critical systems:

# Step 1: Analyze in dry-run mode
./clean-bom-senior.sh /var/www/html --dry-run --verbose

# Step 2: If results look good, apply
./clean-bom-senior.sh /var/www/html --backup --fix-crlf

# Step 3: Verify application works
# ... test your application ...

# Step 4: Clean up backups if all is good
find /var/www/html -name "*.bak" -delete

Restore Specific Files

If something went wrong:

# Restore single file
./clean-bom-senior.sh config.php --restore

# Restore all files of type
find . -name "*.php.bak" -exec ./clean-bom-senior.sh {} --restore \;

Troubleshooting

Problem: "Permission Denied"

Cause: Script or files lack execute/write permissions

Solution:

# Make script executable
chmod +x clean-bom-senior.sh

# Check file permissions
ls -la file.php

# Fix if needed
chmod 644 file.php

Problem: No Changes Made

Cause 1: Files don't have BOM

# Verify with verbose
./clean-bom-senior.sh . --verbose

Cause 2: Using wrong file type filter

# Check supported types
./clean-bom-senior.sh --help | grep file-type

Problem: Excluded Directories Still Processed

Solution: Check exclude syntax

# Correct way
./clean-bom-senior.sh . --exclude .git --exclude node_modules

# Verify with dry-run
./clean-bom-senior.sh . --exclude .git --dry-run --verbose

Problem: Line Endings Not Fixed

Cause: --fix-crlf not included

Solution:

# Include the flag
./clean-bom-senior.sh . --fix-crlf --verbose

# Verify with dry-run first
./clean-bom-senior.sh . --fix-crlf --dry-run

Problem: Backup Files Not Created

Solution: Use --backup flag

# With backup
./clean-bom-senior.sh . --backup

# Verify backups created
ls -la *.bak

Best Practices

1. Always Use Dry-Run First

# Preview
./clean-bom-senior.sh . --dry-run --verbose

# Apply only after verification
./clean-bom-senior.sh . --backup

2. Create Backups for Important Files

./clean-bom-senior.sh . --backup --verbose

3. Use Verbose Mode for Diagnosis

./clean-bom-senior.sh . --verbose

Outputs:

  • File count
  • BOM types found
  • Processing status
  • Error details

4. Test After Changes

For critical systems:

# Clean production files
./clean-bom-senior.sh /app --backup

# Test application
systemctl restart myapp
systemctl status myapp

# If problems, restore
./clean-bom-senior.sh /app --restore

5. Automate Regular Checks

Add to cron for continuous compliance:

# Daily BOM check
0 2 * * * /path/to/clean-bom-senior.sh /var/www --backup --quiet

6. Exclude Directories Appropriately

./clean-bom-senior.sh . \
  --exclude .git \
  --exclude .svn \
  --exclude node_modules \
  --exclude vendor \
  --exclude .cache

Next Steps

Now you have practical examples to work with. For more information:

Clone this wiki locally