AI Crawler · Collection Records

Bot summary · Last 24 hours

xargs — pass results as arguments to execute

A tool that passes piped results as arguments to the next command for batch processing.
Markdown source·Anything to add or correct?

💬 "xargs command"

xargs converts standard input into command arguments. It is essential when combined with find to process large numbers of files.

Use -n/-P to control batch size and parallel execution.

  • Concept: Passes input lines as command arguments to execute.
  • -n: Specifies the number of arguments to pass at once.
  • -P: Specifies the number of parallel processes.
  • -I: Specifies a position with a placeholder.

Frequently used options

OptionDescription
-n <count>Number of arguments to pass at once
-P <count>Number of parallel executions
-I {}Use a placeholder
-0Use NUL delimiter (safe for filenames with spaces)

3 essential command combinations

  • find /var/log/app -name '*.log' -mtime +14 | xargs -r gzip — compress old logs
  • find /srv/data -type f -print0 | xargs -0 -n 100 md5sum > /tmp/md5.txt — safely checksum filenames with spaces
  • cat hosts.txt | xargs -P 8 -I {} ssh {} 'uptime -p' — run commands on multiple servers in parallel

💬 "Please share an easy example for first-time users"

Here are common situations encountered on production servers, organized step by step.

Step 1 · Check the target list

First, see what the targets are.


find /var/log/app -name '*.log' -mtime +14 | head

Step 2 · Execute

Pass them to xargs to process.


find /var/log/app -name '*.log' -mtime +14 -print0 | xargs -0 -r gzip

Step 3 · Verify

Check the results.


ls -1 /var/log/app/*.gz | wc -l

How it is used in practice

1. Compress old logs

Situation — Compress logs older than 14 days to reduce disk usage.


find /var/log/app -name '*.log' -mtime +14 -print0 | xargs -0 -r gzip

Result — The target logs are compressed in bulk, freeing disk space.

Point — The -print0/-0 combination safely handles filenames with spaces.

2. Check multiple servers in parallel

Situation — Check the status of dozens of servers at once.


cat hosts.txt | xargs -P 8 -I {} ssh -o ConnectTimeout=3 {} 'uptime -p'

Result — It runs in parallel, greatly reducing the time required for checks.

Point — Adjust the parallel count based on the target load.

Things to note

  • Filenames with spaces break if processed without -0.
  • Use -r to prevent the command from running when there are no targets.
  • Excessive parallel execution with -P can burden target servers.
  • Before deleting or modifying, check the target list first.

Actual execution screen

This is the result of finding old logs, compressing them in bulk, and checking the count.

!xargs command execution result: bulk log compression

Production server


Source (HTML): https://games.aidebatehub.com/programs/linux/automation/xargs.html This file is an auto-generated Markdown twin of the DevHub document.

Visitor Comments

Loading…
💬 Comments: 0 · Author type: human | Built: 2026-10-30T20:31:00+09:00