loader image

Reply To: Antivirus (Debian/Ubuntu/Xubuntu) Free

What makes us different from other similar websites? Forums Tech Antivirus (Debian/Ubuntu/Xubuntu) Free Reply To: Antivirus (Debian/Ubuntu/Xubuntu) Free

#8165
thumbtak
Moderator

If you want a percentage with an output (txt) with info about the infected files, run the following:

Install:

$ sudo apt update
$ sudo apt install pv

Mask a bash script with the code below:

#!/bin/bash

# --- Configuration ---
SCAN_DIR="/" # Directory to scan (root in this case)
EXCLUDE_DIR="/sys" # Directory to exclude from scan
RESULTS_FILE="clamscan-results.txt" # File to save results to

# --- Check for pv (Pipe Viewer) ---
if ! command -v pv &> /dev/null
then
echo "Error: 'pv' (Pipe Viewer) is not installed."
echo "Please install it first: sudo apt install pv"
exit 1
fi

echo "--- Starting ClamAV Scan with Progress ---"
echo "This might take a while, especially the initial file counting phase."

# --- Step 1: Count the total number of files to be scanned ---
# We use -xdev to stay on the same filesystem (avoiding mounted network drives, etc.)
# We exclude /sys from the count as clamscan will also exclude it.
# 2>/dev/null suppresses permission errors from find.
echo "Counting files to scan... (this may take a few minutes)"
TOTAL_FILES=$(sudo find "$SCAN_DIR" -xdev -type f -print 2>/dev/null | grep -v "$EXCLUDE_DIR/" | wc -l)

if [ "$TOTAL_FILES" -eq 0 ]; then
echo "No files found to scan in $SCAN_DIR (excluding $EXCLUDE_DIR). Exiting."
exit 0
fi

echo "Found $TOTAL_FILES files to scan. Starting ClamAV scan..."

# --- Step 2: Run clamscan with pv progress bar and save results ---
# Create a temporary file for the full scan log
TEMP_SCAN_LOG=$(mktemp)

# Use find to list files, pipe to xargs to run clamscan, then pipe to pv for progress.
# The full clamscan output goes to the temporary log file.
# pv's progress bar goes to stderr (your terminal screen).
sudo find "$SCAN_DIR" -xdev -type f -print 2>/dev/null | grep -v "$EXCLUDE_DIR/" | \
xargs -d '\n' sudo clamscan --bell --exclude-dir="$EXCLUDE_DIR" --stdout --no-summary | \
pv -l -s "$TOTAL_FILES" 2> /dev/stderr > "$TEMP_SCAN_LOG"

# --- Step 3: Filter "found" results from the temporary log to the final results file ---
echo "" # Add a newline after the progress bar finishes
echo "Scan complete. Filtering results..."
grep "found" "$TEMP_SCAN_LOG" > "$RESULTS_FILE"

# --- Step 4: Display a summary and clean up ---
echo "Infected files (if any) are listed in: $RESULTS_FILE"
echo "Full scan log is temporarily in: $TEMP_SCAN_LOG (will be removed)"

# Clean up the temporary log file
rm "$TEMP_SCAN_LOG"

echo "Done."
TAKs Shack