Edit File: get_reseller_top_files.sh
#!/bin/bash # Final output file OUTPUT_FILE="/usr/local/cptools/textfiles/resellers_top_100_files_full_scan.txt" TEMP_DIR=$(mktemp -d) rm -f $OUTPUT_FILE # Ensure cleanup of temporary files trap "rm -rf '$TEMP_DIR'" EXIT # Clear (or create) the final output file > "$OUTPUT_FILE" # Loop through each cPanel user for user_file in /var/cpanel/users/*; do username=$(basename "$user_file") file_output="" # Search for the user's home directory for dir in /home*; do if [ -d "$dir/$username" ]; then # Find files (excluding system paths) temp_output=$(find "$dir/$username" -type f \ \( ! -path "*/.cpanel/*" ! -path "*/.cache/*" ! -path "*/.ssh/*" \) \ -exec du -b {} + 2>/dev/null | awk '$1 ~ /^[0-9]+$/' | sort -nr | head -n 100) if [ -n "$temp_output" ]; then file_output="${file_output}${temp_output}"$'\n' fi fi done if [ -z "$file_output" ]; then [ "$username" != "system" ] && echo "No files found for user: $username" >&2 continue fi # Extract reseller name RESELLER=$(grep -oP '^OWNER=\K.*' "$user_file") [ -z "$RESELLER" ] && RESELLER="Unknown" SAFE_RESELLER=$(echo "$RESELLER" | tr ' ' '_') echo "$file_output" >> "$TEMP_DIR/$SAFE_RESELLER.txt" done # Process each reseller's file list for temp_file in "$TEMP_DIR"/*.txt; do SAFE_RESELLER=$(basename "$temp_file" .txt) echo "Reseller: $SAFE_RESELLER" >> "$OUTPUT_FILE" echo "----------------------------" >> "$OUTPUT_FILE" if [ -s "$temp_file" ]; then sort -nr "$temp_file" | head -100 | while read -r line; do FILE_SIZE_BYTES=$(echo "$line" | awk '{print $1}') FILE_PATH=$(echo "$line" | awk '{$1=""; sub(/^ +/,""); print}') if [[ "$FILE_SIZE_BYTES" =~ ^[0-9]+$ ]]; then FILE_SIZE_HR=$(numfmt --to=iec --suffix=B "$FILE_SIZE_BYTES") echo "$FILE_PATH ---- $FILE_SIZE_HR" >> "$OUTPUT_FILE" else echo "Invalid file size detected in line: $line" >&2 fi done else echo "Skipping empty file: $temp_file" >&2 fi echo "" >> "$OUTPUT_FILE" done echo "Report saved to $OUTPUT_FILE"