ClamAV — Hunt Viruses and Malware from the Command Line
Practical guide to ClamAV — scan files, update signatures with freshclam and detect threats through the clamd daemon on the command line.
ClamAV is the best-known open-source antivirus engine for Unix systems – from mail gateways through file servers to web hosting. Use clamscan for on-demand scans, clamdscan to hand files to the fast background daemon clamd, and freshclam to keep the signature database current. This guide takes you from signature updates through recursive scans and quarantine to daemon management. Make sure freshclam runs regularly – with stale signatures even the best scanner misses fresh threats.
Signature Updates (freshclam)
freshclam — Update virus signature database.
sudo freshclamfreshclam --check <n> — Check for updates n times per day (in daemon mode).
sudo freshclam --check 12freshclam --show-progress — Update signatures with download progress display.
sudo freshclam --show-progressfreshclam --datadir <path> — Use a custom directory for signature databases.
sudo freshclam --datadir /opt/clamav/dbfreshclam -d — Run freshclam as a daemon for automatic updates.
sudo freshclam -dOn-Demand Scanning (clamscan)
clamscan <file> — Scan a single file for threats.
clamscan suspicious-file.zipclamscan -r <dir> — Recursively scan a directory and all subdirectories.
clamscan -r /home/user/Downloadsclamscan -r -i <dir> — Recursively scan and only show infected files.
clamscan -r -i /var/wwwclamscan -r --remove <dir> — Scan and automatically delete infected files (irreversible – use with care).
clamscan -r --remove /tmp/uploadsclamscan -r --move <quarantine> <dir> — Scan and move infected files to a quarantine directory.
clamscan -r --move /quarantine /home/userclamscan -r --copy <quarantine> <dir> — Scan and copy infected files to quarantine (keep originals).
clamscan -r --copy /quarantine /var/wwwclamscan -r -l <logfile> <dir> — Scan and write results to a log file.
clamscan -r -l /var/log/clamav/scan.log /homeclamscan --bell <dir> — Ring a bell when a virus is detected.
clamscan --bell -r /home/userScan Options
clamscan --max-filesize=<size> <dir> — Set maximum file size to scan (default 100M).
clamscan --max-filesize=500M -r /dataclamscan --max-scansize=<size> <dir> — Set maximum data size scanned per file (for archives).
clamscan --max-scansize=1G -r /uploadsclamscan --max-recursion=<n> <dir> — Set max archive extraction depth (default 17).
clamscan --max-recursion=10 -r /tmpclamscan --exclude=<regex> -r <dir> — Exclude files matching a regex pattern.
clamscan --exclude='\.log$' -r /varclamscan --exclude-dir=<regex> -r <dir> — Exclude directories matching a regex pattern.
clamscan --exclude-dir='node_modules' -r /home/user/projectsclamscan --include=<regex> -r <dir> — Only scan files matching a regex pattern.
clamscan --include='\.php$' -r /var/wwwclamscan --no-summary <file> — Suppress the summary at the end of the scan.
clamscan --no-summary -r /tmpDaemon Scanning (clamdscan)
clamdscan <file> — Scan using the clamd daemon (much faster than clamscan).
clamdscan suspicious-file.zipclamdscan -r <dir> — Recursively scan using the daemon.
clamdscan -r /var/wwwclamdscan --multiscan -r <dir> — Parallel scan using multiple daemon threads.
clamdscan --multiscan -r /homeclamdscan --fdpass <file> — Pass file descriptor to clamd (avoids permission issues).
clamdscan --fdpass /root/file.binclamdscan --stream <file> — Stream file to clamd via network (for remote scanning).
clamdscan --stream suspicious-file.zipclamdscan -V — Show clamd version and database info.
clamdscan -VDaemon Management (clamd)
clamd — Start the ClamAV daemon.
sudo clamdclamdtop — Monitor clamd performance in real-time (like top).
clamdtopclamconf — Display ClamAV configuration and database info.
clamconfclamconf --generate-config=clamd.conf — Generate a sample clamd.conf configuration file.
clamconf --generate-config=clamd.conf > /etc/clamav/clamd.confsystemctl status clamav-daemon — Check status of the clamd systemd service.
sudo systemctl status clamav-daemonsystemctl restart clamav-daemon — Restart the clamd daemon.
sudo systemctl restart clamav-daemonDatabase Info
sigtool --info <cvd> — Show info about a signature database file.
sigtool --info /var/lib/clamav/main.cvdsigtool --list-sigs — List all signatures in the loaded databases.
sigtool --list-sigs | wc -lsigtool --find-sigs <name> — Search for a specific signature by name.
sigtool --find-sigs Eicarclamscan --debug 2>&1 | grep 'loaded' — Show number of loaded signatures.
clamscan --debug 2>&1 | grep 'loaded'Common Patterns
clamscan -r -i --move=/quarantine / — Full system scan, show only infected, quarantine threats.
sudo clamscan -r -i --move=/quarantine /clamscan -r -i /var/www -l /var/log/clamav/www-scan.log — Scan web directory and log results for review.
sudo clamscan -r -i /var/www -l /var/log/clamav/www-scan.logfind /uploads -mtime -1 -type f -exec clamscan {} + — Scan only files modified in the last 24 hours.
find /var/www/uploads -mtime -1 -type f -exec clamscan {} +clamscan -r --exclude-dir='^\.git' --include='\.(php|js|html)$' <dir> — Scan only web-relevant files, skip .git directories.
clamscan -r --exclude-dir='^\.git' --include='\.(php|js|html)$' /var/wwwfreshclam && clamscan -r -i /home — Update signatures first, then scan home directories.
sudo freshclam && sudo clamscan -r -i /homeecho 'X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*' > /tmp/eicar.txt && clamscan /tmp/eicar.txt — Create EICAR test file and verify ClamAV detects it.
clamscan /tmp/eicar.txt Conclusion
ClamAV is not a real-time guard like commercial desktop suites; it shines wherever files pass through a controlled chokepoint: mail gateways, upload directories and scheduled cron scans. Pair it with freshclam for fresh signatures and reach for --remove only with care – quarantine via --move is almost always the safer choice, because it lets you recover false positives.
Further Reading
- ClamAV – official documentation – manual and reference
- ClamAV – project site – downloads, signatures and news
- Clam AntiVirus – Wikipedia – background and history