EMZETT.
Login

find, xargs and jq: Processing files and JSON in the terminal

In short: find searches files by criteria, xargs passes results to other commands, and jq filters and shapes JSON data directly in the terminal.

In more detail: These three tools complement pipes (|) into a strong tool chain: find files, process contents further and evaluate structured responses from APIs.

In Depth

find

find . -name "*.log"                 # by name
find . -type f -size +100M            # files over 100 MB
find . -mtime -1                      # changed in the last 24 h
find . -type d -empty                 # empty folders
find . -name "*.tmp" -delete          # delete found ones (careful!)
find . -name "*.py" -exec wc -l {} +  # command on each file

Check the output first without -delete. With -iname you ignore case. A faster and simpler modern alternative is fd.

xargs

xargs takes input lines and turns them into arguments of a command:

find . -name "*.txt" | xargs grep "error"
find . -name "*.jpg" -print0 | xargs -0 -P4 -I{} convert {} -resize 50% {}.small.jpg

-print0 and -0 handle file names with spaces safely, -P4 runs four jobs in parallel, -I{} places the argument at a specific spot.

jq: processing JSON

curl -s https://api.example.com/users | jq '.'              # pretty-print
cat data.json | jq '.[] | {name, mail}'                    # select fields
jq '.items[] | select(.price > 10) | .name' data.json      # filter
jq -r '.[].name' data.json                                 # raw (no quotes)
jq 'length' data.json                                      # count

. is the current element, [] iterates over lists, | joins filters, select() filters, -r prints raw text.

Together

Example: search all JSON files in the folder for a field:

find . -name "*.json" -print0 | xargs -0 jq -r '.title'

Always secure scripts with quotes around variables, test commands on copies and use --dry-run options where available. For large amounts of data specialised tools (ripgrep, fd, databases) are faster.

See also: grep, sed and awk, curl and wget, Finding files