Console9

How to combine grep with find, awk, and xargs

Build powerful text processing pipelines by combining grep with find, awk, xargs, and other Unix commands.

Chain grep with find, awk, xargs, sed, and other Unix commands to build text search and processing pipelines.

Step-by-Step

1. Find Files by Name and Search Their Contents

find /etc -name "*.conf" -exec grep -l "listen" {} +

The find command locates all .conf files, and grep -l prints filenames that contain "listen". The + passes multiple files to grep at once for efficiency.

2. Use xargs to Pipe Filenames into grep

find /var/log -name "*.log" -mtime -1 | xargs grep -l "error"

The find command selects log files modified in the last 24 hours. xargs passes the filenames to grep in batches.

3. Extract Specific Fields with grep and awk

grep "404" /var/log/nginx/access.log | awk '{print $7}' | sort | uniq -c | sort -rn | head -20

This pipeline finds all 404 errors, extracts the URL path (field 7), counts unique URLs, and displays the top 20 most-requested missing pages.

4. Replace Text Found by grep Using sed

grep -rl "old-domain.com" /var/www/ | xargs sed -i 's/old-domain.com/new-domain.com/g'

The grep -rl finds all files containing the old domain. xargs sed -i performs in-place replacement in each file.

Common Issues

xargs fails with filenames containing spaces— Use null-delimited output: find . -name "*.conf" -print0 | xargs -0 grep "pattern". The -print0 and -0 flags use null bytes instead of newlines as delimiters.