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.

File and Folder Move Management on macOS

Efficiently manage file and folder move operations across your Roundfleet deployment with enterprise-grade safety features, automatic backup creation, and comprehensive audit capabilities. This tutorial transforms basic mv commands into robust data relocation solutions.

Understanding Enterprise File Move Operations

Enterprise file moving requires more than basic relocation, demanding:

  • Safety validation to prevent data loss during moves
  • Automatic backup creation before destructive operations
  • Permission preservation during relocation
  • Rollback capabilities for failed operations
  • Rename protection against naming conflicts
  • Audit logging for compliance tracking

Core Move Operations

Basic File Move

#!/bin/bash

# Simple file move with validation
move_file() {
    local source="$1"
    local destination="$2"
    
    # Validate source exists
    if [[ ! -f "$source" ]]; then
        echo "Error: Source file '$source' not found"
        return 1
    fi
    
    # Create destination directory if needed
    local dest_dir=$(dirname "$destination")
    mkdir -p "$dest_dir"
    
    # Move file
    if mv "$source" "$destination"; then
        echo "Successfully moved '$source' to '$destination'"
        return 0
    else
        echo "Failed to move '$source' to '$destination'"
        return 1
    fi
}

# Usage example
# move_file "/Users/admin/document.pdf" "/Users/shared/documents/document.pdf"

Basic Directory Move

#!/bin/bash

# Directory move with verification
move_directory() {
    local source="$1"
    local destination="$2"
    
    # Validate source directory exists
    if [[ ! -d "$source" ]]; then
        echo "Error: Source directory '$source' not found"
        return 1
    fi
    
    # Move directory
    if mv "$source" "$destination"; then
        echo "Successfully moved directory '$source' to '$destination'"
        return 0
    else
        echo "Failed to move directory '$source' to '$destination'"
        return 1
    fi
}

# Usage example
# move_directory "/Users/admin/project" "/Users/shared/projects/project"

File and Folder Rename

#!/bin/bash

# Rename file or folder
rename_item() {
    local current_path="$1"
    local new_name="$2"
    
    # Validate source exists
    if [[ ! -e "$current_path" ]]; then
        echo "Error: Source '$current_path' not found"
        return 1
    fi
    
    local parent_dir=$(dirname "$current_path")
    local new_path="$parent_dir/$new_name"
    
    # Check if new name already exists
    if [[ -e "$new_path" ]]; then
        echo "Error: '$new_path' already exists"
        return 1
    fi
    
    # Rename item
    if mv "$current_path" "$new_path"; then
        echo "Successfully renamed '$(basename "$current_path")' to '$new_name'"
        return 0
    else
        echo "Failed to rename '$(basename "$current_path")' to '$new_name'"
        return 1
    fi
}

# Usage examples
# rename_item "/Users/admin/oldfile.txt" "newfile.txt"
# rename_item "/Users/admin/old_folder" "new_folder"

Enterprise Move Management System

#!/bin/bash

# Roundfleet Enterprise File Move Management System
# Comprehensive file and folder moving with enterprise features

# Configuration
SCRIPT_NAME="Roundfleet Move Manager"
VERSION="1.0.0"
LOG_FILE="/var/log/roundfleet_move_operations.log"
BACKUP_DIR="/var/backups/roundfleet/move_operations"
TEMP_DIR="/tmp/roundfleet_move"
MAX_FILE_SIZE="10G"
ALLOWED_EXTENSIONS=(".pdf" ".docx" ".xlsx" ".pptx" ".txt" ".png" ".jpg" ".gif" ".mp4" ".mov")
RESTRICTED_PATHS=("/System" "/usr/bin" "/usr/sbin" "/private/var" "/Applications")
PROTECTED_DIRECTORIES=("/Users/Shared" "/Library" "/var")
BUSINESS_HOURS_START=9
BUSINESS_HOURS_END=17

# Create necessary directories
mkdir -p "$TEMP_DIR"
mkdir -p "$BACKUP_DIR"
mkdir -p "$(dirname "$LOG_FILE")"

# Logging function
log_operation() {
    local level="$1"
    local message="$2"
    local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
    echo "[$timestamp] [$level] $message" | tee -a "$LOG_FILE"
}

# Check if current time is within business hours
is_business_hours() {
    local current_hour=$(date +%H)
    if [[ $current_hour -ge $BUSINESS_HOURS_START && $current_hour -lt $BUSINESS_HOURS_END ]]; then
        return 0
    else
        return 1
    fi
}

# Validate file extension
is_allowed_extension() {
    local file="$1"
    local extension="${file##*.}"
    extension=".$extension"
    
    for allowed in "${ALLOWED_EXTENSIONS[@]}"; do
        if [[ "$extension" == "$allowed" ]]; then
            return 0
        fi
    done
    return 1
}

# Check if path is restricted
is_restricted_path() {
    local path="$1"
    for restricted in "${RESTRICTED_PATHS[@]}"; do
        if [[ "$path" == "$restricted"* ]]; then
            return 0
        fi
    done
    return 1
}

# Check if destination is in protected directory
is_protected_destination() {
    local path="$1"
    for protected in "${PROTECTED_DIRECTORIES[@]}"; do
        if [[ "$path" == "$protected"* ]]; then
            return 0
        fi
    done
    return 1
}

# Get file/directory size
get_item_size() {
    local item="$1"
    if [[ -f "$item" ]]; then
        stat -f%z "$item" 2>/dev/null
    elif [[ -d "$item" ]]; then
        du -sk "$item" 2>/dev/null | awk '{print $1 * 1024}'
    else
        echo "0"
    fi
}

# Convert bytes to human readable format
format_size() {
    local bytes="$1"
    local sizes=("B" "KB" "MB" "GB" "TB")
    local unit=0
    
    while [[ $bytes -gt 1024 && $unit -lt 4 ]]; do
        bytes=$((bytes / 1024))
        ((unit++))
    done
    
    echo "${bytes}${sizes[$unit]}"
}

# Calculate checksum for verification
calculate_checksum() {
    local item="$1"
    if [[ -f "$item" ]]; then
        shasum -a 256 "$item" 2>/dev/null | awk '{print $1}'
    elif [[ -d "$item" ]]; then
        find "$item" -type f -exec shasum -a 256 {} \; 2>/dev/null | shasum -a 256 | awk '{print $1}'
    else
        echo ""
    fi
}

# Create backup before move operation
create_move_backup() {
    local source="$1"
    local operation_id="$2"
    local timestamp=$(date '+%Y%m%d_%H%M%S')
    
    if [[ -e "$source" ]]; then
        local item_name=$(basename "$source")
        local backup_path="$BACKUP_DIR/${operation_id}_${item_name}_backup_$timestamp"
        
        if cp -R "$source" "$backup_path"; then
            log_operation "INFO" "Backup created: $backup_path"
            echo "$backup_path"
            return 0
        else
            log_operation "ERROR" "Failed to create backup for: $source"
            return 1
        fi
    else
        log_operation "WARNING" "Source does not exist for backup: $source"
        return 1
    fi
}

# Restore from backup
restore_from_backup() {
    local backup_path="$1"
    local restore_destination="$2"
    
    if [[ -e "$backup_path" ]]; then
        local restore_dir=$(dirname "$restore_destination")
        mkdir -p "$restore_dir"
        
        if cp -R "$backup_path" "$restore_destination"; then
            log_operation "INFO" "Restored from backup: $backup_path -> $restore_destination"
            return 0
        else
            log_operation "ERROR" "Failed to restore from backup: $backup_path"
            return 1
        fi
    else
        log_operation "ERROR" "Backup not found: $backup_path"
        return 1
    fi
}

# Check for naming conflicts
check_naming_conflict() {
    local destination="$1"
    local resolve_method="${2:-prompt}"  # prompt, overwrite, rename, skip
    
    if [[ -e "$destination" ]]; then
        case "$resolve_method" in
            "overwrite")
                log_operation "WARNING" "Will overwrite existing item: $destination"
                return 0
                ;;
            "rename")
                local counter=1
                local base_name="${destination%.*}"
                local extension="${destination##*.}"
                local new_destination
                
                while true; do
                    if [[ "$base_name" == "$destination" ]]; then
                        # No extension
                        new_destination="${destination}_${counter}"
                    else
                        new_destination="${base_name}_${counter}.${extension}"
                    fi
                    
                    if [[ ! -e "$new_destination" ]]; then
                        echo "$new_destination"
                        log_operation "INFO" "Resolved naming conflict: $destination -> $new_destination"
                        return 0
                    fi
                    ((counter++))
                done
                ;;
            "skip")
                log_operation "INFO" "Skipping due to naming conflict: $destination"
                return 2
                ;;
            "prompt"|*)
                log_operation "ERROR" "Naming conflict detected: $destination"
                return 1
                ;;
        esac
    else
        echo "$destination"
        return 0
    fi
}

# Verify move operation integrity
verify_move_integrity() {
    local source_backup="$1"
    local destination="$2"
    
    if [[ -e "$source_backup" && -e "$destination" ]]; then
        local backup_checksum=$(calculate_checksum "$source_backup")
        local dest_checksum=$(calculate_checksum "$destination")
        
        if [[ "$backup_checksum" == "$dest_checksum" ]]; then
            log_operation "INFO" "Move integrity verification passed: $destination"
            return 0
        else
            log_operation "ERROR" "Move integrity verification failed: $destination"
            return 1
        fi
    else
        log_operation "WARNING" "Cannot verify move integrity - missing backup or destination"
        return 1
    fi
}

# Enhanced file move with enterprise features
enterprise_move_file() {
    local source="$1"
    local destination="$2"
    local conflict_resolution="${3:-prompt}"  # prompt, overwrite, rename, skip
    local create_backup="${4:-true}"
    local verify_integrity="${5:-true}"
    
    local operation_id=$(date +%s)
    log_operation "INFO" "Starting file move operation [$operation_id]: $source -> $destination"
    
    # Pre-flight validations
    if [[ ! -f "$source" ]]; then
        log_operation "ERROR" "Source file not found: $source"
        return 1
    fi
    
    # Check if source is restricted
    if is_restricted_path "$source"; then
        log_operation "ERROR" "Source path is restricted: $source"
        return 1
    fi
    
    # Check if destination is restricted
    if is_restricted_path "$destination"; then
        log_operation "ERROR" "Destination path is restricted: $destination"
        return 1
    fi
    
    # Validate file extension
    if ! is_allowed_extension "$source"; then
        log_operation "WARNING" "File extension not in allowed list: $source"
    fi
    
    # Check file size
    local file_size=$(get_item_size "$source")
    local max_size_bytes=$(echo "$MAX_FILE_SIZE" | sed 's/G//' | awk '{print $1 * 1024 * 1024 * 1024}')
    
    if [[ $file_size -gt $max_size_bytes ]]; then
        log_operation "ERROR" "File too large: $(format_size $file_size) > $MAX_FILE_SIZE"
        return 1
    fi
    
    # Business hours check for large files (>100MB)
    if [[ $file_size -gt 104857600 ]] && ! is_business_hours; then
        log_operation "WARNING" "Large file move outside business hours: $(format_size $file_size)"
    fi
    
    # Check for naming conflicts
    local resolved_destination
    resolved_destination=$(check_naming_conflict "$destination" "$conflict_resolution")
    local conflict_result=$?
    
    if [[ $conflict_result -eq 1 ]]; then
        log_operation "ERROR" "Naming conflict resolution failed: $destination"
        return 1
    elif [[ $conflict_result -eq 2 ]]; then
        log_operation "INFO" "Move operation skipped due to conflict: $destination"
        return 0
    fi
    
    destination="$resolved_destination"
    
    # Create destination directory
    local dest_dir=$(dirname "$destination")
    if ! mkdir -p "$dest_dir"; then
        log_operation "ERROR" "Failed to create destination directory: $dest_dir"
        return 1
    fi
    
    # Create backup if requested
    local backup_path=""
    if [[ "$create_backup" == "true" ]]; then
        backup_path=$(create_move_backup "$source" "$operation_id")
        if [[ $? -ne 0 ]]; then
            log_operation "ERROR" "Failed to create backup for: $source"
            return 1
        fi
    fi
    
    # Perform the move operation
    if mv "$source" "$destination"; then
        log_operation "INFO" "File moved successfully: $(format_size $file_size)"
        
        # Verify integrity if requested and backup exists
        if [[ "$verify_integrity" == "true" && -n "$backup_path" ]]; then
            if ! verify_move_integrity "$backup_path" "$destination"; then
                log_operation "ERROR" "Move integrity verification failed, attempting restore"
                
                # Restore from backup
                if [[ -n "$backup_path" ]]; then
                    restore_from_backup "$backup_path" "$source"
                    rm -f "$destination"
                fi
                return 1
            fi
        fi
        
        # Log successful operation
        log_operation "SUCCESS" "File move completed [$operation_id]: $source -> $destination"
        return 0
    else
        log_operation "ERROR" "Move operation failed [$operation_id]: $source -> $destination"
        return 1
    fi
}

# Enhanced directory move with enterprise features
enterprise_move_directory() {
    local source="$1"
    local destination="$2"
    local conflict_resolution="${3:-prompt}"
    local create_backup="${4:-true}"
    local verify_integrity="${5:-true}"
    
    local operation_id=$(date +%s)
    log_operation "INFO" "Starting directory move operation [$operation_id]: $source -> $destination"
    
    # Pre-flight validations
    if [[ ! -d "$source" ]]; then
        log_operation "ERROR" "Source directory not found: $source"
        return 1
    fi
    
    # Check if source is restricted
    if is_restricted_path "$source"; then
        log_operation "ERROR" "Source path is restricted: $source"
        return 1
    fi
    
    # Check if destination is restricted
    if is_restricted_path "$destination"; then
        log_operation "ERROR" "Destination path is restricted: $destination"
        return 1
    fi
    
    # Calculate directory size and file count
    local dir_size=$(get_item_size "$source")
    local file_count=$(find "$source" -type f | wc -l)
    
    log_operation "INFO" "Directory stats - Size: $(format_size $dir_size), Files: $file_count"
    
    # Business hours check for large directories (>1GB or >1000 files)
    if [[ $dir_size -gt 1073741824 || $file_count -gt 1000 ]] && ! is_business_hours; then
        log_operation "WARNING" "Large directory move outside business hours"
    fi
    
    # Check for naming conflicts
    local resolved_destination
    resolved_destination=$(check_naming_conflict "$destination" "$conflict_resolution")
    local conflict_result=$?
    
    if [[ $conflict_result -eq 1 ]]; then
        log_operation "ERROR" "Naming conflict resolution failed: $destination"
        return 1
    elif [[ $conflict_result -eq 2 ]]; then
        log_operation "INFO" "Move operation skipped due to conflict: $destination"
        return 0
    fi
    
    destination="$resolved_destination"
    
    # Create parent directory for destination
    local dest_parent=$(dirname "$destination")
    if ! mkdir -p "$dest_parent"; then
        log_operation "ERROR" "Failed to create destination parent directory: $dest_parent"
        return 1
    fi
    
    # Create backup if requested
    local backup_path=""
    if [[ "$create_backup" == "true" ]]; then
        backup_path=$(create_move_backup "$source" "$operation_id")
        if [[ $? -ne 0 ]]; then
            log_operation "ERROR" "Failed to create backup for: $source"
            return 1
        fi
    fi
    
    # Perform the move operation
    if mv "$source" "$destination"; then
        log_operation "INFO" "Directory moved successfully: $(format_size $dir_size)"
        
        # Verify integrity if requested and backup exists
        if [[ "$verify_integrity" == "true" && -n "$backup_path" ]]; then
            if ! verify_move_integrity "$backup_path" "$destination"; then
                log_operation "ERROR" "Directory move integrity verification failed, attempting restore"
                
                # Restore from backup
                if [[ -n "$backup_path" ]]; then
                    restore_from_backup "$backup_path" "$source"
                    rm -rf "$destination"
                fi
                return 1
            fi
        fi
        
        # Log successful operation
        log_operation "SUCCESS" "Directory move completed [$operation_id]: $source -> $destination"
        return 0
    else
        log_operation "ERROR" "Directory move operation failed [$operation_id]: $source -> $destination"
        return 1
    fi
}

# Enhanced rename with enterprise features
enterprise_rename() {
    local current_path="$1"
    local new_name="$2"
    local create_backup="${3:-true}"
    
    local operation_id=$(date +%s)
    log_operation "INFO" "Starting rename operation [$operation_id]: $(basename "$current_path") -> $new_name"
    
    # Validate source exists
    if [[ ! -e "$current_path" ]]; then
        log_operation "ERROR" "Source not found: $current_path"
        return 1
    fi
    
    # Check if source is restricted
    if is_restricted_path "$current_path"; then
        log_operation "ERROR" "Source path is restricted: $current_path"
        return 1
    fi
    
    local parent_dir=$(dirname "$current_path")
    local new_path="$parent_dir/$new_name"
    
    # Check if new name already exists
    if [[ -e "$new_path" ]]; then
        log_operation "ERROR" "Target name already exists: $new_path"
        return 1
    fi
    
    # Validate new name (no special characters that could cause issues)
    if [[ "$new_name" =~ [/\\:*?"<>|] ]]; then
        log_operation "ERROR" "Invalid characters in new name: $new_name"
        return 1
    fi
    
    # Create backup if requested
    local backup_path=""
    if [[ "$create_backup" == "true" ]]; then
        backup_path=$(create_move_backup "$current_path" "$operation_id")
        if [[ $? -ne 0 ]]; then
            log_operation "WARNING" "Failed to create backup for rename operation"
        fi
    fi
    
    # Perform rename
    if mv "$current_path" "$new_path"; then
        log_operation "SUCCESS" "Rename completed [$operation_id]: $(basename "$current_path") -> $new_name"
        return 0
    else
        log_operation "ERROR" "Rename operation failed [$operation_id]: $(basename "$current_path") -> $new_name"
        return 1
    fi
}

# Bulk move operations with progress monitoring
bulk_move_operation() {
    local operation_type="$1"  # "file", "directory", or "rename"
    local operations_file="$2" # File containing operation definitions
    local conflict_resolution="${3:-rename}"
    
    if [[ ! -f "$operations_file" ]]; then
        log_operation "ERROR" "Operations file not found: $operations_file"
        return 1
    fi
    
    local total_operations=$(grep -v '^#\|^$' "$operations_file" | wc -l)
    local current_operation=0
    local success_count=0
    local failure_count=0
    
    log_operation "INFO" "Starting bulk move operation - Total operations: $total_operations"
    
    while IFS='|' read -r source destination; do
        # Skip empty lines and comments
        [[ -z "$source" || "$source" =~ ^#.* ]] && continue
        
        ((current_operation++))
        
        # Trim whitespace
        source=$(echo "$source" | xargs)
        destination=$(echo "$destination" | xargs)
        
        echo "Processing [$current_operation/$total_operations]: $(basename "$source")"
        
        case "$operation_type" in
            "file")
                if enterprise_move_file "$source" "$destination" "$conflict_resolution"; then
                    ((success_count++))
                else
                    ((failure_count++))
                fi
                ;;
            "directory")
                if enterprise_move_directory "$source" "$destination" "$conflict_resolution"; then
                    ((success_count++))
                else
                    ((failure_count++))
                fi
                ;;
            "rename")
                if enterprise_rename "$source" "$destination"; then
                    ((success_count++))
                else
                    ((failure_count++))
                fi
                ;;
        esac
        
        # Progress update
        local progress=$((current_operation * 100 / total_operations))
        echo "Progress: $progress% ($success_count successful, $failure_count failed)"
        
    done < "$operations_file"
    
    log_operation "SUCCESS" "Bulk move completed - Success: $success_count, Failed: $failure_count"
    return $failure_count
}

# Generate move operations report
generate_move_report() {
    local report_file="/tmp/roundfleet_move_report_$(date +%Y%m%d_%H%M%S).txt"
    
    {
        echo "Roundfleet Move Operations Report"
        echo "Generated: $(date)"
        echo "Hostname: $(hostname)"
        echo "=============================="
        echo ""
        
        echo "Recent Move Operations (Last 24 hours):"
        if [[ -f "$LOG_FILE" ]]; then
            local yesterday=$(date -v-1d '+%Y-%m-%d')
            grep "$yesterday\|$(date '+%Y-%m-%d')" "$LOG_FILE" | tail -50
        else
            echo "No log file found"
        fi
        
        echo ""
        echo "System Information:"
        echo "Available Space:"
        df -h | grep -E "^/dev/"
        
        echo ""
        echo "Backup Directory Status:"
        if [[ -d "$BACKUP_DIR" ]]; then
            echo "Backup Location: $BACKUP_DIR"
            echo "Backup Count: $(find "$BACKUP_DIR" -type f | wc -l)"
            echo "Backup Size: $(du -sh "$BACKUP_DIR" 2>/dev/null | awk '{print $1}')"
        else
            echo "Backup directory not found"
        fi
        
        echo ""
        echo "Move Operation Statistics:"
        if [[ -f "$LOG_FILE" ]]; then
            echo "Total Operations: $(grep -c "move operation\|rename operation" "$LOG_FILE" 2>/dev/null || echo "0")"
            echo "Successful: $(grep -c "SUCCESS.*completed" "$LOG_FILE" 2>/dev/null || echo "0")"
            echo "Failed: $(grep -c "ERROR.*failed" "$LOG_FILE" 2>/dev/null || echo "0")"
        fi
        
    } > "$report_file"
    
    echo "Move operations report saved to: $report_file"
    log_operation "INFO" "Move report generated: $report_file"
}

# Cleanup old backups
cleanup_old_backups() {
    local retention_days="${1:-30}"
    
    log_operation "INFO" "Starting backup cleanup - Retention: $retention_days days"
    
    if [[ -d "$BACKUP_DIR" ]]; then
        local deleted_count=0
        
        # Find and delete backups older than retention period
        while IFS= read -r -d '' backup_file; do
            if rm -rf "$backup_file"; then
                ((deleted_count++))
                log_operation "INFO" "Deleted old backup: $(basename "$backup_file")"
            fi
        done < <(find "$BACKUP_DIR" -type f -mtime +$retention_days -print0 2>/dev/null)
        
        log_operation "SUCCESS" "Backup cleanup completed - Deleted: $deleted_count items"
    else
        log_operation "WARNING" "Backup directory not found: $BACKUP_DIR"
    fi
}

# Main move management function
main() {
    local action="${1:-help}"
    
    case "$action" in
        "move-file")
            local source="$2"
            local destination="$3"
            local conflict_resolution="${4:-prompt}"
            local create_backup="${5:-true}"
            local verify="${6:-true}"
            
            if [[ -z "$source" || -z "$destination" ]]; then
                echo "Usage: $0 move-file <source> <destination> [conflict_resolution] [create_backup] [verify_integrity]"
                exit 1
            fi
            
            enterprise_move_file "$source" "$destination" "$conflict_resolution" "$create_backup" "$verify"
            ;;
        "move-directory")
            local source="$2"
            local destination="$3"
            local conflict_resolution="${4:-prompt}"
            local create_backup="${5:-true}"
            local verify="${6:-true}"
            
            if [[ -z "$source" || -z "$destination" ]]; then
                echo "Usage: $0 move-directory <source> <destination> [conflict_resolution] [create_backup] [verify_integrity]"
                exit 1
            fi
            
            enterprise_move_directory "$source" "$destination" "$conflict_resolution" "$create_backup" "$verify"
            ;;
        "rename")
            local current_path="$2"
            local new_name="$3"
            local create_backup="${4:-true}"
            
            if [[ -z "$current_path" || -z "$new_name" ]]; then
                echo "Usage: $0 rename <current_path> <new_name> [create_backup]"
                exit 1
            fi
            
            enterprise_rename "$current_path" "$new_name" "$create_backup"
            ;;
        "bulk-files")
            local operations_file="$2"
            local conflict_resolution="${3:-rename}"
            
            if [[ -z "$operations_file" ]]; then
                echo "Usage: $0 bulk-files <operations_file> [conflict_resolution]"
                exit 1
            fi
            
            bulk_move_operation "file" "$operations_file" "$conflict_resolution"
            ;;
        "bulk-directories")
            local operations_file="$2"
            local conflict_resolution="${3:-rename}"
            
            if [[ -z "$operations_file" ]]; then
                echo "Usage: $0 bulk-directories <operations_file> [conflict_resolution]"
                exit 1
            fi
            
            bulk_move_operation "directory" "$operations_file" "$conflict_resolution"
            ;;
        "bulk-rename")
            local operations_file="$2"
            
            if [[ -z "$operations_file" ]]; then
                echo "Usage: $0 bulk-rename <operations_file>"
                exit 1
            fi
            
            bulk_move_operation "rename" "$operations_file"
            ;;
        "report")
            generate_move_report
            ;;
        "cleanup-backups")
            local retention_days="${2:-30}"
            cleanup_old_backups "$retention_days"
            ;;
        "help"|*)
            echo "$SCRIPT_NAME v$VERSION"
            echo "Enterprise File and Folder Move Management"
            echo ""
            echo "Usage: $0 <action> [options]"
            echo ""
            echo "Actions:"
            echo "  move-file <source> <destination>           - Move single file"
            echo "  move-directory <source> <destination>      - Move directory"
            echo "  rename <current_path> <new_name>           - Rename file or folder"
            echo "  bulk-files <operations_file>               - Bulk move files"
            echo "  bulk-directories <operations_file>         - Bulk move directories"
            echo "  bulk-rename <operations_file>              - Bulk rename operations"
            echo "  report                                      - Generate operations report"
            echo "  cleanup-backups [retention_days]           - Clean old backups (default: 30 days)"
            echo "  help                                        - Show this help message"
            echo ""
            echo "Conflict Resolution Options:"
            echo "  prompt     - Stop and prompt for resolution (default)"
            echo "  overwrite  - Overwrite existing items"
            echo "  rename     - Auto-rename with counter suffix"
            echo "  skip       - Skip conflicting operations"
            echo ""
            echo "Operations File Format (source|destination per line):"
            echo "  /path/to/source1.txt|/path/to/destination1.txt"
            echo "  /path/to/source2.pdf|/path/to/destination2.pdf"
            echo ""
            echo "Features:"
            echo "  • Safety validation and automatic backup creation"
            echo "  • Conflict resolution with multiple strategies"
            echo "  • Business hours compliance checking"
            echo "  • Comprehensive audit logging"
            echo "  • Integrity verification"
            echo "  • Bulk operations with progress monitoring"
            echo "  • Automatic backup cleanup"
            ;;
    esac
}

# Execute main function with all arguments
main "$@"

Quick Reference Commands

Single File Operations

# Move file with full enterprise features
./move_manager.sh move-file "/Users/admin/document.pdf" "/Users/shared/documents/document.pdf"

# Move file with overwrite conflict resolution
./move_manager.sh move-file "/source/file.txt" "/destination/file.txt" overwrite

# Move file with auto-rename on conflicts
./move_manager.sh move-file "/source/file.txt" "/destination/file.txt" rename

# Move file without backup creation
./move_manager.sh move-file "/source/file.txt" "/destination/file.txt" prompt false

Directory Operations

# Move directory with all contents
./move_manager.sh move-directory "/Users/admin/project" "/Users/shared/projects/project"

# Move directory with rename on conflicts
./move_manager.sh move-directory "/source/folder" "/destination/folder" rename

Rename Operations

# Rename file
./move_manager.sh rename "/Users/admin/oldfile.txt" "newfile.txt"

# Rename directory
./move_manager.sh rename "/Users/admin/old_folder" "new_folder"

# Rename without backup
./move_manager.sh rename "/path/to/item" "new_name" false

Bulk Operations

# Create operations file for bulk moves
cat > /tmp/move_operations.txt << EOF
/Users/admin/doc1.pdf|/Users/shared/documents/doc1.pdf
/Users/admin/doc2.pdf|/Users/shared/documents/doc2.pdf
/Users/admin/doc3.pdf|/Users/shared/documents/doc3.pdf
EOF

# Execute bulk file moves
./move_manager.sh bulk-files "/tmp/move_operations.txt" rename

# Create rename operations file
cat > /tmp/rename_operations.txt << EOF
/Users/admin/oldname1.txt|newname1.txt
/Users/admin/oldname2.pdf|newname2.pdf
EOF

# Execute bulk renames
./move_manager.sh bulk-rename "/tmp/rename_operations.txt"

Integration Examples

JAMF Pro Integration

#!/bin/bash

# JAMF Pro script for enterprise file moves
# Parameters: $4 = operation_type, $5 = source_path, $6 = destination_path, $7 = conflict_resolution

OPERATION_TYPE="$4"
SOURCE_PATH="$5"
DESTINATION_PATH="$6"
CONFLICT_RESOLUTION="${7:-rename}"

# Download move manager if not present
if [[ ! -f "/usr/local/bin/roundfleet_move_manager.sh" ]]; then
    curl -o "/usr/local/bin/roundfleet_move_manager.sh" "https://scripts.roundfleet.com/move_manager.sh"
    chmod +x "/usr/local/bin/roundfleet_move_manager.sh"
fi

# Execute move operation
case "$OPERATION_TYPE" in
    "file")
        /usr/local/bin/roundfleet_move_manager.sh move-file "$SOURCE_PATH" "$DESTINATION_PATH" "$CONFLICT_RESOLUTION"
        ;;
    "directory")
        /usr/local/bin/roundfleet_move_manager.sh move-directory "$SOURCE_PATH" "$DESTINATION_PATH" "$CONFLICT_RESOLUTION"
        ;;
    "rename")
        /usr/local/bin/roundfleet_move_manager.sh rename "$SOURCE_PATH" "$DESTINATION_PATH"
        ;;
    *)
        echo "Invalid operation type: $OPERATION_TYPE"
        exit 1
        ;;
esac

# Generate report
/usr/local/bin/roundfleet_move_manager.sh report

exit $?

Configuration Profile for Move Policies

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>PayloadContent</key>
    <array>
        <dict>
            <key>PayloadType</key>
            <string>com.roundfleet.move.policy</string>
            <key>PayloadIdentifier</key>
            <string>com.roundfleet.move.policy.main</string>
            <key>PayloadDisplayName</key>
            <string>Roundfleet Move Policy</string>
            <key>MaxFileSize</key>
            <string>10G</string>
            <key>AllowedExtensions</key>
            <array>
                <string>.pdf</string>
                <string>.docx</string>
                <string>.xlsx</string>
                <string>.pptx</string>
            </array>
            <key>ConflictResolution</key>
            <string>rename</string>
            <key>BusinessHoursOnly</key>
            <true/>
            <key>CreateBackups</key>
            <true/>
            <key>BackupRetentionDays</key>
            <integer>30</integer>
        </dict>
    </array>
</dict>
</plist>

Security and Compliance Features

Automated Backup Management

# Schedule daily backup cleanup
cat > /etc/cron.daily/roundfleet-move-cleanup << 'EOF'
#!/bin/bash
/usr/local/bin/roundfleet_move_manager.sh cleanup-backups 30
EOF

chmod +x /etc/cron.daily/roundfleet-move-cleanup

Compliance Reporting

# Generate compliance report for auditors
generate_compliance_report() {
    local report_file="/var/reports/move_compliance_$(date +%Y%m%d).csv"
    
    echo "Timestamp,Operation_ID,User,Source,Destination,Size,Status,Backup_Path" > "$report_file"
    
    # Parse log file for move operations
    grep "move operation\|rename operation" "$LOG_FILE" | while read line; do
        # Extract relevant information and format as CSV
        echo "$line" | awk -F' - ' '{print $1","$2}' >> "$report_file"
    done
    
    echo "Compliance report generated: $report_file"
}

Troubleshooting

Common Issues and Solutions

IssueCauseSolution
Permission deniedInsufficient privilegesRun with sudo or check file permissions
Cross-device linkMoving across filesystemsUse copy + delete instead of mv
File in useApplication has file openClose application or use force option
Naming conflictDestination existsUse conflict resolution strategy
Operation timeoutLarge directory/slow diskBreak into smaller operations

Recovery Procedures

# List available backups
ls -la /var/backups/roundfleet/move_operations/

# Restore from specific backup
./move_manager.sh restore-backup "/var/backups/roundfleet/move_operations/backup_file" "/restore/destination"

# Check move operation logs
tail -f /var/log/roundfleet_move_operations.log

# Search for failed operations
grep "ERROR" /var/log/roundfleet_move_operations.log

Best Practices

  1. Always test on single device before fleet deployment
  2. Use backup creation for critical operations
  3. Monitor disk space before large move operations
  4. Choose appropriate conflict resolution for your use case
  5. Regular backup cleanup to manage disk usage
  6. Review logs regularly for failed operations
  7. Use business hours restrictions for resource-intensive moves
  8. Implement proper file naming conventions to avoid conflicts

This enterprise move management system provides comprehensive safety, backup, and recovery features while maintaining the simplicity and efficiency of basic move operations for effective fleet management.

Related Tutorials

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 Open Management on macOS

Comprehensive guide for enterprise file and folder opening operations on Roundfleet devices with security validation, application control, and automated access 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

File and Folder Copy Management on macOS

Comprehensive guide for enterprise file and folder copying operations on Roundfleet devices with safety validation, integrity verification, and automated distribution management.

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 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

Manage FileVault Encryption on macOS

Learn how to forcefully enable, disable, and manage FileVault disk encryption on macOS devices using command-line scripts for enterprise security management.

Jul 7, 2025

Delete Mobile Accounts on macOS Devices

Learn how to efficiently delete mobile accounts on macOS devices for user management, device reassignment, and organizational cleanup.

Jul 7, 2025