A powerful and safe command-line tool for deleting files and folders with comprehensive safety features, logging, and flexible pattern matching.
- Overview
- Features
- Installation
- Usage
- Options
- Examples
- Safety Features
- Output Formats
- Logging
- Best Practices
- Troubleshooting
- Contributing
cull is an enhanced bash script designed to safely and efficiently delete files and folders based on patterns, with multiple safety mechanisms to prevent accidental data loss. It provides features like dry-run mode, interactive confirmation, exclusion lists, depth control, and comprehensive logging.
- Pattern-Based Deletion: Delete files and folders using glob patterns
- Multiple Pattern Types: Support for files, folders, and mixed types
- Exclusion Lists: Protect important files/folders from deletion
- Depth Control: Limit search depth in directory structures
- Dry Run Mode: Preview deletions without making changes
- Interactive Mode: Confirm each deletion individually
- Preview Mode: See what will be deleted before execution
- Comprehensive Logging: Track all operations with timestamps
- Multiple Output Formats: Plain text or JSON output
- Force Mode: Bypass safety checks when needed
- β Dry-run mode to preview changes
- β Interactive confirmation prompts
- β Exclusion pattern support
- β Preview before deletion
- β Comprehensive error handling
- β Detailed logging with timestamps
- β Summary reports after execution
- Bash 4.0 or higher
- Standard Unix utilities (find, rm, date)
- Clone or download the script:
git clone <repository-url>
cd cull- Make the script executable:
chmod +x delete.sh- (Optional) Add to your PATH for global access:
sudo ln -s $(pwd)/delete.sh /usr/local/bin/cull./delete.sh [OPTIONS]# Preview what would be deleted (dry run)
./delete.sh --files "*.tmp" --dry-run
# Delete temporary files interactively
./delete.sh --files "*.tmp" --interactive
# Delete with logging
./delete.sh --files "*.log" --log cleanup.log| Option | Description | Example |
|---|---|---|
--files PATTERN |
Delete files matching pattern | --files "*.tmp" |
--folders PATTERN |
Delete folders matching pattern | --folders "temp" |
--types PATTERN |
Delete files/directories matching pattern | --types "*.log" |
| Option | Description |
|---|---|
--dry-run |
Simulate deletion without actually deleting |
--interactive |
Prompt for confirmation before each deletion |
--preview |
Display list of items to be deleted |
--force |
Bypass safety checks and warnings |
| Option | Description | Example |
|---|---|---|
--exclude PATTERN |
Exclude patterns from deletion (can use multiple times) | --exclude "important" |
--depth NUMBER |
Limit search depth in directory structure | --depth 2 |
| Option | Description | Example |
|---|---|---|
--log FILE |
Generate log file with timestamps | --log deletion.log |
--format FORMAT |
Output format: plain or json |
--format json |
-h, --help |
Display help message | --help |
Delete temporary files with preview and logging:
./delete.sh --files "*.tmp" --preview --log cleanup.log --dry-runOutput:
[2024-01-15 10:30:00] [INFO] Starting deletion operation...
[2024-01-15 10:30:00] [INFO] Preview: Items that will be deleted (5 items):
- ./cache/file1.tmp
- ./cache/file2.tmp
- ./logs/error.tmp
- ./temp/data.tmp
- ./tmp/backup.tmp
[2024-01-15 10:30:00] [INFO] Would delete: ./cache/file1.tmp
...
Delete log files with confirmation prompts:
./delete.sh --types "*.log" --interactive --depth 2Interactive Prompt:
Delete './logs/app.log'? [y/N]: y
[2024-01-15 10:31:00] [INFO] Deleted file: ./logs/app.log
Delete './logs/error.log'? [y/N]: n
[2024-01-15 10:31:05] [INFO] Skipped: ./logs/error.log
Delete temporary files but exclude important directories:
./delete.sh --files "*.tmp" --exclude "important_folder" --exclude "backup" --log cleanup.logDelete cache folders up to 2 levels deep:
./delete.sh --folders "cache" --depth 2 --previewGet output in JSON format for programmatic processing:
./delete.sh --files "*.tmp" --format json --dry-runOutput:
{"timestamp":"2024-01-15 10:32:00","level":"INFO","message":"Starting deletion operation..."}
{"timestamp":"2024-01-15 10:32:00","level":"INFO","message":"Would delete: ./file.tmp"}
{"items":[{"path":"./file.tmp"}]}Multiple patterns with exclusions and logging:
./delete.sh \
--files "*.tmp" \
--folders "temp" \
--exclude "important_folder" \
--exclude "backup" \
--log deletion_log.txt \
--dry-run \
--previewForce delete without prompts (use with caution):
./delete.sh --folders "cache" --force --log cleanup.logAlways test your deletion patterns with --dry-run first:
./delete.sh --files "*.tmp" --dry-runThis shows what would be deleted without actually deleting anything.
Get confirmation for each item:
./delete.sh --files "*.tmp" --interactiveSee all items before deletion:
./delete.sh --files "*.tmp" --previewProtect important files and folders:
./delete.sh --files "*.tmp" --exclude "important" --exclude "backup"Human-readable output with timestamps and log levels:
[2024-01-15 10:30:00] [INFO] Starting deletion operation...
[2024-01-15 10:30:00] [INFO] Deleted file: ./file.tmp
Machine-readable output for automation:
{"timestamp":"2024-01-15 10:30:00","level":"INFO","message":"Deleted file: ./file.tmp"}When using --log, the script creates a detailed log file:
==========================================
Deletion Log - 2024-01-15 10:30:00
==========================================
Dry Run: false
Interactive: true
Force: false
Files Pattern: *.tmp
Exclude Patterns: important backup
Max Depth: 2
==========================================
[2024-01-15 10:30:00] [INFO] Starting deletion operation...
[2024-01-15 10:30:01] [INFO] Deleted file: ./file1.tmp
[2024-01-15 10:30:02] [INFO] Deleted file: ./file2.tmp
- Track all deletion operations
- Audit trail for compliance
- Debugging failed deletions
- Historical record of cleanup activities
# Test your pattern first
./delete.sh --files "*.tmp" --dry-run# See what will be deleted
./delete.sh --files "*.tmp" --preview# Keep a record of deletions
./delete.sh --files "*.tmp" --log cleanup.log# Protect important files
./delete.sh --files "*.tmp" --exclude "important" --exclude "backup"# Maximum safety
./delete.sh --files "*.tmp" --dry-run --preview --interactive --log cleanup.log# Avoid deep recursive searches
./delete.sh --files "*.tmp" --depth 3Solution:
- Verify your pattern syntax (use quotes for patterns with wildcards)
- Check if files/folders exist in the current directory
- Try using
--previewto see what matches
./delete.sh --files "*.tmp" --previewSolution:
- Check file/folder permissions
- Run with appropriate user permissions
- Some system files may require sudo (use with extreme caution)
Solution:
- Check if files are in use by other processes
- Verify write permissions
- Review the log file for detailed error information
Solution:
- Use
--dry-runand--previewto test patterns - Remember that patterns are glob patterns, not regex
- Use quotes around patterns:
--files "*.tmp"
*.tmp- All files ending with .tmptemp*- All items starting with "temp"*cache*- All items containing "cache"temp- Exact match for "temp"
# Delete all temporary files
./delete.sh --files "*.tmp"
# Delete all log files
./delete.sh --files "*.log"
# Delete folders named "cache"
./delete.sh --folders "cache"
# Delete items matching multiple patterns
./delete.sh --types "*.tmp" --types "*.log"0- Success (all deletions completed)1- Error (failed deletions or invalid arguments)
After execution, the script provides a summary:
=== Deletion Summary ===
Total items found: 10
Items successfully deleted: 8
Failed deletions: 2
- ./protected/file.tmp
- ./locked/data.tmp
Contributions are welcome! Please feel free to submit issues or pull requests.
Always use --dry-run and --preview before performing actual deletions. This script permanently deletes files and folders. Use with caution!
This script is provided as-is for educational and practical use. Use at your own risk.
Created as an enhanced deletion tool with safety features and comprehensive logging capabilities.
Remember: When in doubt, use --dry-run first! π‘οΈ