Safe Output for Pipelines
Like find and fd, locate can produce unsafe output if filenames contain spaces. Use null-byte delimiters for bulletproof pipelines.
The Problem with Default Output
# If a result is "/home/user/My Documents/report.txt"
# default output: My Documents/report.txt
# xargs splits at space → tries "My" and "Documents/report.txt" separately
locate "report" | xargs grep "keyword" # BROKEN if spaces in paths
Null-Delimited Output (-0)
-0 separates results with null bytes (\0) instead of newlines:
locate -0 "report" | xargs -0 grep "keyword"
Safe while read Loop
For complex multi-step operations:
locate -0 -e "*.conf" | while IFS= read -r -d '' file; do
echo "Checking: $file"
grep -l "insecure" "$file"
done
Passing Results to find for Metadata Filtering
locate cannot filter by size, age, or permissions. After getting a quick list from locate, refine with find:
# All php.ini files, check which ones were modified recently
locate -e -0 "php.ini" | xargs -0 find -newer /etc/passwd -print
Counting Lines Safely
# Count results
locate "*.log" | wc -l
# Count existing results only
locate -e "*.log" | wc -l