You're absolutely right—scanning the entire root (/) is usually overkill, slow, and unnecessary. Most system binaries and core OS files (like /bin, /sbin, /lib, /usr, etc.) are immutable and managed by package managers, so they’re unlikely to be infected unless the system is already compromised. Even then, ClamAV isn’t designed to detect rootkits or kernel-level malware (use tools like rkhunter or chkrootkit for that).
✅ Best Directories to Scan
Focus on user data and common infection vectors:
/home # User files (downloads, documents, etc.)
/var/www # Web server files (if running a web server)
/tmp # Temporary files (malware often dropped here)
/root # Admin home directory
/media/* # External drives
/mnt/* # Mounted filesystems
/opt # Third-party software
🧹 Directories to Exclude
Avoid scanning these (safe to skip):
/dev # Device files (not real files)
/proc # Virtual filesystem for kernel/process info
/sys # Virtual filesystem for hardware info
/run # Runtime state (short-lived files)
/lost+found # Filesystem recovery files
🔍 Optimized Scan Command
Here’s a faster scan that skips unnecessary paths and focuses on user data:
sudo clamdscan -r --fdpass --infected \
--exclude-dir=/dev \
--exclude-dir=/proc \
--exclude-dir=/sys \
--exclude-dir=/run \
--exclude-dir=/lost+found \
--exclude-dir=/boot \
--exclude-dir=/usr \
--exclude-dir=/lib \
--exclude-dir=/bin \
--exclude-dir=/sbin \
--exclude-dir=/etc \
--exclude-dir=/snap \
/home /var/www /tmp /root /media /mnt
Explanation:
--exclude-dir skips paths you don’t care about.
- Scans only user/data-heavy directories.
- Uses
clamdscan (daemon-based, faster than clamscan).
🧪 Test First
Before running a full scan, test a small directory to verify:
sudo clamdscan -r --fdpass --infected /home/your-username/Downloads
⏰ Performance Tips
- Scan during off-peak hours (e.g., nightly via
cron).
- Exclude large directories like
/var/log (unless you suspect log injection).
- Use
nice and ionice to reduce system impact:
sudo nice -n 19 ionice -c 3 clamdscan -r --fdpass --infected /home
This lowers CPU/I/O priority so the scan doesn’t slow down your system.
📅 Example Cron Job
Edit your root cron:
sudo crontab -e
Add:
0 2 * * * nice -n 19 ionice -c 3 /usr/bin/clamdscan -r --fdpass --infected --no-summary --exclude-dir=/dev --exclude-dir=/proc --exclude-dir=/sys --exclude-dir=/run --exclude-dir=/lost+found /home /var/www /tmp /root /media /mnt > /var/log/clamav/nightly-scan.log 2>&1
This:
- Runs nightly at 2 AM.
- Logs results to
/var/log/clamav/nightly-scan.log.
- Uses
nice/ionice to minimize resource usage.
🚫 Bonus: Exclude Specific File Types
If you want to skip non-executable files:
--exclude='^.*\.(gif|jpg|png|mp3|mp4|avi|iso|deb|rpm|pdf)$'
Add this to avoid scanning media or package files (adjust as needed).
🧠 Final Advice
- ClamAV is not a silver bullet: It’s great for detecting known malware (e.g., email attachments, web uploads) but not advanced threats.
- Combine with other tools: Use
fail2ban for intrusion prevention, auditd for file integrity monitoring, and a firewall.
Let me know if you want a script to automate exclusions or send scan results to email!