A powerful command-line tool for safely deleting files and folders with comprehensive safety features
npm install cullA 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)
1. Clone or download the script:
``bash`
git clone
cd cull
2. Make the script executable:
`bash`
chmod +x delete.sh
3. (Optional) Add to your PATH for global access:
`bash`
sudo ln -s $(pwd)/delete.sh /usr/local/bin/cull
`bash`
./delete.sh [OPTIONS]
`bashPreview what would be deleted (dry run)
./delete.sh --files "*.tmp" --dry-run
📖 Options
$3
| 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" |$3
| 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 |$3
| 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 |$3
| 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 |💡 Examples
$3
Delete temporary files with preview and logging:
`bash
./delete.sh --files "*.tmp" --preview --log cleanup.log --dry-run
`Output:
`
[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
...
`$3
Delete log files with confirmation prompts:
`bash
./delete.sh --types "*.log" --interactive --depth 2
`Interactive 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
`$3
Delete temporary files but exclude important directories:
`bash
./delete.sh --files "*.tmp" --exclude "important_folder" --exclude "backup" --log cleanup.log
`$3
Delete cache folders up to 2 levels deep:
`bash
./delete.sh --folders "cache" --depth 2 --preview
`$3
Get output in JSON format for programmatic processing:
`bash
./delete.sh --files "*.tmp" --format json --dry-run
`Output:
`json
{"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"}]}
`$3
Multiple patterns with exclusions and logging:
`bash
./delete.sh \
--files "*.tmp" \
--folders "temp" \
--exclude "important_folder" \
--exclude "backup" \
--log deletion_log.txt \
--dry-run \
--preview
`$3
Force delete without prompts (use with caution):
`bash
./delete.sh --folders "cache" --force --log cleanup.log
`🛡️ Safety Features
$3
Always test your deletion patterns with
--dry-run first:`bash
./delete.sh --files "*.tmp" --dry-run
`This shows what would be deleted without actually deleting anything.
$3
Get confirmation for each item:
`bash
./delete.sh --files "*.tmp" --interactive
`$3
See all items before deletion:
`bash
./delete.sh --files "*.tmp" --preview
`$3
Protect important files and folders:
`bash
./delete.sh --files "*.tmp" --exclude "important" --exclude "backup"
`📊 Output Formats
$3
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
`$3
Machine-readable output for automation:
`json
{"timestamp":"2024-01-15 10:30:00","level":"INFO","message":"Deleted file: ./file.tmp"}
`📝 Logging
$3
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
`$3
- Track all deletion operations
- Audit trail for compliance
- Debugging failed deletions
- Historical record of cleanup activities
🎯 Best Practices
$3
`bash
Test your pattern first
./delete.sh --files "*.tmp" --dry-run
`$3
`bash
See what will be deleted
./delete.sh --files "*.tmp" --preview
`$3
`bash
Keep a record of deletions
./delete.sh --files "*.tmp" --log cleanup.log
`$3
`bash
Protect important files
./delete.sh --files "*.tmp" --exclude "important" --exclude "backup"
`$3
`bash
Maximum safety
./delete.sh --files "*.tmp" --dry-run --preview --interactive --log cleanup.log
`$3
`bash
Avoid deep recursive searches
./delete.sh --files "*.tmp" --depth 3
`🔧 Troubleshooting
$3
Solution:
- Verify your pattern syntax (use quotes for patterns with wildcards)
- Check if files/folders exist in the current directory
- Try using
--preview to see what matches`bash
./delete.sh --files "*.tmp" --preview
`$3
Solution:
- Check file/folder permissions
- Run with appropriate user permissions
- Some system files may require sudo (use with extreme caution)
$3
Solution:
- Check if files are in use by other processes
- Verify write permissions
- Review the log file for detailed error information
$3
Solution:
- Use
--dry-run and --preview to test patterns
- Remember that patterns are glob patterns, not regex
- Use quotes around patterns: --files "*.tmp"📋 Pattern Matching
$3
-
*.tmp - All files ending with .tmp
- temp* - All items starting with "temp"
- cache - All items containing "cache"
- temp - Exact match for "temp"$3
`bash
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"
`🔄 Exit Codes
-
0 - Success (all deletions completed)
- 1 - Error (failed deletions or invalid arguments)📄 Summary Report
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
`🤝 Contributing
Contributions are welcome! Please feel free to submit issues or pull requests.
⚠️ Warning
Always use
--dry-run and --preview before performing actual deletions. This script permanently deletes files and folders. Use with caution!📜 License
This script is provided as-is for educational and practical use. Use at your own risk.
🙏 Acknowledgments
Created as an enhanced deletion tool with safety features and comprehensive logging capabilities.
---
Remember: When in doubt, use
--dry-run` first! 🛡️