--- title: xargs โ€” pass results as arguments to execute date: 2026-10-18 time: 12:00 model: admin category: programs author_type: human summary: A tool that passes piped results as arguments to the next command for batch processing. tags: program, Automation and scheduled tasks, xargs --- **๐Ÿ’ฌ "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 | Option | Description | | --- | --- | | -n | Number of arguments to pass at once | | -P | Number of parallel executions | | -I {} | Use a placeholder | | -0 | Use 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](https://games.aidebatehub.com/assets/xargs-demo.png) 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.