Tutorial

New updates and improvements to Roundfleet.

Important notice

The code examples and scripts provided in these tutorials are for educational purposes only. Roundfleet is not responsible for any issues, damages, or security vulnerabilities that may arise from the use, modification, or implementation of these examples. Always review and test code in a secure environment before using it in production systems.

Delete Files by Size on macOS

Learn how to find and delete large files on Mac devices based on size criteria. This helps free up storage space and improve device performance when storage is running low.

List Large Files

Find and list files larger than a specified size:

#!/bin/bash

# Get current user context
CURRENT_USER=$(stat -f "%Su" /dev/console)
CURRENT_USER_UID=$(id -u "$CURRENT_USER")

# Configuration
SEARCH_PATH="/Users/$CURRENT_USER/Desktop"
SIZE_LIMIT="100"  # Size in kilobytes

echo "Searching for files larger than ${SIZE_LIMIT}KB in: $SEARCH_PATH"

# List large files
launchctl asuser $CURRENT_USER_UID sudo -iu "$CURRENT_USER" \
    find "$SEARCH_PATH" -type f -size +${SIZE_LIMIT}k -print

Delete Large Files

Find and delete files larger than specified size:

#!/bin/bash

# Get current user context
CURRENT_USER=$(stat -f "%Su" /dev/console)
CURRENT_USER_UID=$(id -u "$CURRENT_USER")

# Configuration
SEARCH_PATH="/Users/$CURRENT_USER/Downloads"
SIZE_LIMIT="50"  # Size in kilobytes

echo "⚠️  WARNING: This will permanently delete files larger than ${SIZE_LIMIT}KB"
echo "Searching in: $SEARCH_PATH"

# List and delete large files
launchctl asuser $CURRENT_USER_UID sudo -iu "$CURRENT_USER" \
    find "$SEARCH_PATH" -type f -size +${SIZE_LIMIT}k -print -delete

echo "✅ Large files deleted successfully"

Enhanced File Cleanup Script

Script with safety checks and detailed reporting:

#!/bin/bash

# Function to cleanup large files safely
cleanup_large_files() {
    local search_path="$1"
    local size_limit="$2"
    local dry_run="${3:-false}"
    
    # Get current user context
    local current_user=$(stat -f "%Su" /dev/console)
    local current_user_uid=$(id -u "$current_user")
    
    if [[ ! -d "$search_path" ]]; then
        echo "❌ Error: Directory not found: $search_path"
        return 1
    fi
    
    echo "=== Large Files Cleanup ==="
    echo "📁 Path: $search_path"
    echo "📏 Size limit: ${size_limit}KB"
    echo "👤 User: $current_user"
    echo "🔍 Dry run: $dry_run"
    echo "=========================="
    
    # Find large files
    local files_command="find '$search_path' -type f -size +${size_limit}k"
    
    if [[ "$dry_run" == "true" ]]; then
        echo "📋 Files that would be deleted:"
        launchctl asuser $current_user_uid sudo -iu "$current_user" \
            bash -c "$files_command -ls"
        
        local count=$(launchctl asuser $current_user_uid sudo -iu "$current_user" \
            bash -c "$files_command | wc -l")
        echo "📊 Total files found: $count"
    else
        echo "🗑️  Deleting large files..."
        launchctl asuser $current_user_uid sudo -iu "$current_user" \
            bash -c "$files_command -print -delete"
        echo "✅ Cleanup completed"
    fi
}

# Configuration
SEARCH_PATH="/Users/$(stat -f "%Su" /dev/console)/Downloads"
SIZE_LIMIT="100"
DRY_RUN="true"  # Set to "false" to actually delete files

# Run cleanup
cleanup_large_files "$SEARCH_PATH" "$SIZE_LIMIT" "$DRY_RUN"

Enterprise Storage Management

Comprehensive storage cleanup for enterprise environments:

#!/bin/bash

# Enterprise storage cleanup configuration
CLEANUP_LOCATIONS=(
    "/Users/*/Downloads:50"
    "/Users/*/Desktop:100"
    "/Users/*/Documents:200"
    "/tmp:10"
)

# Function to format file size
format_size() {
    local size=$1
    if [[ $size -gt 1048576 ]]; then
        echo "$(( size / 1048576 ))GB"
    elif [[ $size -gt 1024 ]]; then
        echo "$(( size / 1024 ))MB"
    else
        echo "${size}KB"
    fi
}

# Function to get directory size
get_directory_size() {
    local dir="$1"
    if [[ -d "$dir" ]]; then
        du -sk "$dir" 2>/dev/null | cut -f1
    else
        echo "0"
    fi
}

# Enterprise cleanup execution
echo "🏢 Roundfleet Enterprise Storage Cleanup"
echo "======================================"
echo "Generated: $(date)"
echo "Device: $(hostname)"

total_freed=0
current_user=$(stat -f "%Su" /dev/console)
current_user_uid=$(id -u "$current_user")

for location_config in "${CLEANUP_LOCATIONS[@]}"; do
    IFS=':' read -r location_pattern size_limit <<< "$location_config"
    
    echo ""
    echo "📁 Processing: $location_pattern (>${size_limit}KB)"
    
    # Expand pattern for user directories
    for location in $location_pattern; do
        if [[ -d "$location" ]]; then
            echo "  🔍 Scanning: $location"
            
            # Get before size
            before_size=$(get_directory_size "$location")
            
            # Find and delete large files
            deleted_files=$(launchctl asuser $current_user_uid sudo -iu "$current_user" \
                find "$location" -type f -size +${size_limit}k -print -delete 2>/dev/null | wc -l)
            
            # Get after size
            after_size=$(get_directory_size "$location")
            freed=$((before_size - after_size))
            total_freed=$((total_freed + freed))
            
            if [[ $deleted_files -gt 0 ]]; then
                echo "  ✅ Deleted: $deleted_files files"
                echo "  💾 Freed: $(format_size $freed)"
            else
                echo "  ℹ️  No large files found"
            fi
        fi
    done
done

echo ""
echo "======================================"
echo "📊 Summary:"
echo "  Total space freed: $(format_size $total_freed)"
echo "  Cleanup completed: $(date)"

if [[ $total_freed -gt 0 ]]; then
    echo "✅ Storage cleanup successful"
else
    echo "ℹ️  No cleanup needed - storage usage is optimal"
fi

Safe Cleanup with Backup Option

Script with optional backup before deletion:

#!/bin/bash

# Function to backup large files before deletion
backup_and_delete() {
    local search_path="$1"
    local size_limit="$2"
    local backup_enabled="${3:-false}"
    
    local current_user=$(stat -f "%Su" /dev/console)
    local current_user_uid=$(id -u "$current_user")
    local backup_dir="/tmp/roundfleet_backup_$(date +%Y%m%d_%H%M%S)"
    
    echo "🔄 Processing: $search_path"
    
    if [[ "$backup_enabled" == "true" ]]; then
        echo "📦 Creating backup directory: $backup_dir"
        mkdir -p "$backup_dir"
        
        # Find and backup large files
        launchctl asuser $current_user_uid sudo -iu "$current_user" \
            find "$search_path" -type f -size +${size_limit}k -exec cp {} "$backup_dir/" \;
        
        echo "✅ Files backed up to: $backup_dir"
    fi
    
    # Delete large files
    local deleted_count=$(launchctl asuser $current_user_uid sudo -iu "$current_user" \
        find "$search_path" -type f -size +${size_limit}k -print -delete | wc -l)
    
    echo "🗑️  Deleted $deleted_count large files"
    
    if [[ "$backup_enabled" == "true" && $deleted_count -gt 0 ]]; then
        echo "💡 Backup location: $backup_dir"
        echo "⏰ Backup will be auto-removed in 7 days"
        
        # Schedule backup cleanup (requires launchd or cron)
        echo "find '$backup_dir' -type f -mtime +7 -delete" > /tmp/cleanup_backup.sh
        chmod +x /tmp/cleanup_backup.sh
    fi
}

# Configuration
SEARCH_PATH="/Users/$(stat -f "%Su" /dev/console)/Downloads"
SIZE_LIMIT="100"
BACKUP_ENABLED="true"

# Execute safe cleanup
backup_and_delete "$SEARCH_PATH" "$SIZE_LIMIT" "$BACKUP_ENABLED"

Usage with Roundfleet

  1. Configure search path and size limit in the script
  2. Choose between dry-run and actual deletion modes
  3. Consider enabling backup option for safety
  4. Deploy through Roundfleet's remote script execution
  5. Monitor results and freed space in action history

Size Units and Examples

UnitDescriptionExample Use Case
+50kFiles larger than 50KBSmall document cleanup
+10MFiles larger than 10MBMedia file cleanup
+100MFiles larger than 100MBLarge file cleanup
+1GFiles larger than 1GBArchive cleanup

Common Cleanup Targets

DirectoryTypical Size LimitPurpose
Downloads50MBRemove large downloads
Desktop100MBClean desktop files
Documents200MBArchive old documents
Temp folders10MBSystem cleanup

Safety Considerations

⚠️ CRITICAL WARNING: Files deleted with these scripts are permanently removed and cannot be recovered from Trash.

  • Test first: Always run with dry-run mode enabled initially
  • Backup important data: Consider backup option for valuable files
  • Verify paths: Double-check search directories before execution
  • User context: Scripts run in current user context for security

Troubleshooting

Permission denied: Ensure proper user context with launchctl asuser Directory not found: Verify search paths exist before running No files found: Check size limits and directory contents


Note: Always validate scripts on test systems before bulk deployment. Permanent file deletion requires careful consideration and backup planning.

Related Tutorials

Hide and Unhide Files and Folders on macOS

Control file and folder visibility on Mac devices using chflags commands and Finder settings for enhanced security and organization.

Jul 7, 2025

File Size and Storage Analysis on macOS

Learn how to analyze file and folder sizes, monitor disk usage, identify storage hogs, and implement comprehensive storage management on macOS devices using command-line tools for effective storage optimization.

Jul 7, 2025

Deploy ZIP Files to Macs Using URLs

Learn how to automate the download and extraction of ZIP files from URLs on macOS devices for enterprise application deployment.

Jul 7, 2025

Storage Management on macOS

Monitor disk usage, implement storage policies, and optimize storage performance across macOS devices using advanced storage management, automated cleanup, and enterprise analytics for organizational storage infrastructure.

Jul 7, 2025

File Protection on macOS

Implement comprehensive file and application protection on macOS devices using immutable flags, access controls, and enterprise security policies for organizational data protection.

Jul 7, 2025

File Ownership Management on macOS

Learn how to change file and folder ownership, manage permissions, and implement comprehensive ownership policies on macOS devices using command-line scripts for secure file system management.

Jul 7, 2025

File and Folder Move Management on macOS

Comprehensive guide for enterprise file and folder move operations on Roundfleet devices with safety validation, rollback capabilities, and automated relocation management.

Jul 7, 2025

File and Folder Permissions Management on macOS

Learn how to manage file and folder permissions, understand ownership models, set access controls, and implement enterprise-grade permission policies on macOS devices using command-line scripts.

Jul 7, 2025