Skip to main content

Counting and Stats

Count Results (-c)

-c suppresses the list of filenames and prints only a count:

# How many files contain "nginx" in the path?
locate -c nginx

# How many PHP config files are indexed?
locate -c "php.ini"

# Confirm a file exists (exit 0 if count > 0)
if [ "$(locate -c -e "\nginx.conf")" -gt 0 ]; then
echo "nginx.conf found"
fi

Database Statistics (-S)

-S prints statistics about the database rather than searching:

locate -S

Example output:

Database /var/lib/mlocate/mlocate.db:
2,341 directories
41,893 files
2,156,789 bytes in file names
876,543 bytes used to store database

This tells you:

  • How many files are indexed
  • How big the database is
  • (Implicitly) whether updatedb has run recently — a very small count suggests it has been pruned or hasn't run yet

Practical Inventory Queries

# Count all Python files on the system
locate -c "*.py"

# Count all config files
locate -c "*.conf"

# Count files in a specific directory
locate -c "/var/log"

# Compare before/after installing a package
locate -c "" > before.txt
sudo apt install some-package && sudo updatedb
locate -c "" > after.txt
echo "New files: $(($(cat after.txt) - $(cat before.txt)))"