Automated cleanup scripts for removing old files from /sftp1/data/csv and /sftp1/data/xls directories.
Removes files older than 180 days (6 months).
Use case: Regular maintenance to keep only recent data.
Removes files older than 14 days (2 weeks), keeping only the last 2 weeks of data.
Use case: Aggressive cleanup when disk space is critical.
Both scripts include built-in safety mechanisms:
-
Test Mode by Default (
DELETE_MODE=0)- Shows what would be deleted WITHOUT actually deleting
- Displays file count and total size
- Shows sample of oldest files
-
Delete Mode (
DELETE_MODE=1)- Actually deletes the files
- Only activate after reviewing test mode output
# Run in test mode (default, safe)
./cleanup_files.shExpected output:
[2025-11-16 10:30:00] *** TEST MODE - NO FILES WILL BE DELETED ***
[2025-11-16 10:30:00] csv: Found 611 files (1.25GB)
[2025-11-16 10:30:00] csv: Would delete 611 files (1.25GB)
[2025-11-16 10:30:00] TOTAL: 935 files, 2.10GB
Check the test output carefully:
- Verify file counts make sense
- Check total size to be deleted
- Review sample of oldest files shown
# Edit script and change:
DELETE_MODE=0 → DELETE_MODE=1
# Then run:
./cleanup_files.sh# Copy scripts to system location
sudo cp cleanup_files.sh /usr/local/bin/
sudo cp cleanup_keep2weeks.sh /usr/local/bin/
sudo chmod +x /usr/local/bin/cleanup_*.sh# Edit crontab
crontab -e
# Run cleanup_files.sh daily at 2 AM
0 2 * * * /usr/local/bin/cleanup_files.sh >> /var/log/cleanup-6months.log 2>&1
# Run cleanup_keep2weeks.sh weekly on Sunday at 3 AM
0 3 * * 0 /usr/local/bin/cleanup_keep2weeks.sh >> /var/log/cleanup-2weeks.log 2>&1IMPORTANT: Remember to set DELETE_MODE=1 in the script before scheduling in cron!
Each script can be customized by editing these variables:
BASE_DIR="/sftp1/data" # Base directory path
DAYS_OLD=180 # Retention period (180 or 14)
VERBOSE=1 # 1=show details, 0=minimal output
DELETE_MODE=0 # 0=test mode, 1=actual deletionBoth scripts display:
- Total files found matching age criteria
- Total size in human-readable format (KB/MB/GB)
- Sample of oldest files (when VERBOSE=1)
- Per-folder breakdown
- Overall summary
✅ Supports:
- Files with spaces in names
- Special characters in filenames
- Nested subdirectories
- Human-readable size formats
- Uses file modification time (
mtime) -mtime +Nmeans "older than N days"- Always test first with
DELETE_MODE=0 - Check logs regularly when running via cron
No files found but expecting some?
- Check directory paths are correct
- Verify file modification dates:
ls -lht /sftp1/data/csv/ | head - Ensure script has read permissions
Permission denied errors?
- Run with appropriate user permissions
- Check directory and file ownership
Size showing 0B?
- Files may be too new (within retention period)
- Verify with:
find /sftp1/data/csv -type f -mtime +180 -ls
- Always test first - Run with
DELETE_MODE=0before enabling deletion - Review logs regularly - Check cron job logs for issues
- Keep backups - Ensure important data is backed up elsewhere
- Monitor disk space - Verify cleanup is freeing expected space
- Document retention policy - Know why you chose 6 months or 2 weeks
If files are accidentally deleted:
- Check system backups immediately
- Stop further write operations to the disk
- Use recovery tools:
testdisk,photorec,extundelete - Contact system administrator
Internal use only.