{"id":462,"date":"2026-10-14T14:51:32","date_gmt":"2026-10-14T14:51:32","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/2015\/12\/12\/20-xargs-commands-examples-in-linux-unix\/"},"modified":"2026-10-14T14:51:32","modified_gmt":"2026-10-14T14:51:32","slug":"20-xargs-commands-examples-in-linux-unix","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/20-xargs-commands-examples-in-linux-unix\/","title":{"rendered":"xargs in Linux: Practical Commands, Safe Patterns, and Pipeline Workflows"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><em>Originally published on 2015-12-12; fully revised and updated.<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2015\/12\/xargs-commands-examples-in-.png\" alt=\"xargs-commands\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>xargs<\/code> command builds and runs commands from standard input. It is especially useful for DevOps and Linux engineers who need to process long lists of files, IDs, or paths in shell pipelines. In this tutorial, you\u2019ll learn how <code>xargs<\/code> works, when to use key options, and how to avoid common mistakes with whitespace and destructive commands.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How xargs works in a shell pipeline<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">At a high level, one command produces items, <code>xargs<\/code> groups\/parses those items, and then executes a target command with the resulting arguments. If you run <code>xargs<\/code> without specifying a command, it typically uses <code>echo<\/code> as the default command.<\/p>\n\n\n\n<pre class=\"mermaid\">\nflowchart TD\n    A[Producer command emits items] --> B[Choose delimiter strategy]\n    B --> C[xargs parses input]\n    C --> D[Apply batch and replacement rules]\n    D --> E[Build target command arguments]\n    E --> F[Execute target command]\n    F --> G{More input items}\n    G -- yes --> C\n    G -- no --> H[Pipeline completes]\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Quick option comparison<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Option<\/th><th>What it does<\/th><th>Typical use<\/th><th>Portability note<\/th><\/tr><\/thead><tbody><tr><td><code>-n N<\/code><\/td><td>Use at most N arguments per command run<\/td><td>Batching large input lists<\/td><td>Widely available<\/td><\/tr><tr><td><code>-L N<\/code><\/td><td>Use at most N input lines per run<\/td><td>Line-oriented batching<\/td><td>Widely available<\/td><\/tr><tr><td><code>-0<\/code><\/td><td>Read NUL-delimited input<\/td><td>Safe filename handling with <code>find -print0<\/code><\/td><td>Common on GNU\/BSD, not required by POSIX<\/td><\/tr><tr><td><code>-d DELIM<\/code><\/td><td>Use a custom delimiter<\/td><td>Newline-only splitting or custom records<\/td><td>GNU extension<\/td><\/tr><tr><td><code>-I REPL<\/code><\/td><td>Replace placeholder token in command template<\/td><td>Insert item in the middle of a command<\/td><td>Widely available<\/td><\/tr><tr><td><code>-P N<\/code><\/td><td>Run up to N processes in parallel<\/td><td>Parallelize independent tasks<\/td><td>Implementation-dependent<\/td><\/tr><tr><td><code>-t<\/code> \/ <code>-p<\/code><\/td><td>Trace command \/ prompt before run<\/td><td>Safer dry-run and confirmation<\/td><td>Widely available<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Practical xargs command examples<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1) Basic stdin behavior<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This reproduces the classic interactive behavior: type input, then press <code>Ctrl+D<\/code> to end stdin.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs language-bash\">xargs\nHi,\nWelcome to TGS.\n^D\nHi, Welcome to TGS.<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\">2) Limit arguments per command with -n<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php language-bash\"><span class=\"hljs-keyword\">echo<\/span> a b c d e f | xargs\n<span class=\"hljs-comment\"># a b c d e f<\/span>\n\n<span class=\"hljs-keyword\">echo<\/span> a b c d e f | xargs -n <span class=\"hljs-number\">3<\/span>\n<span class=\"hljs-comment\"># a b c<\/span>\n<span class=\"hljs-comment\"># d e f<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">3) Split only on newlines with -d &#8216;\\n&#8217; (GNU)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This helps preserve spaces inside each line item.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php language-bash\">printf <span class=\"hljs-string\">'alpha\\nbeta gamma\\n'<\/span> | xargs -d <span class=\"hljs-string\">'\\n'<\/span> -n <span class=\"hljs-number\">1<\/span> printf <span class=\"hljs-string\">'&#91;%s]\\n'<\/span>\n<span class=\"hljs-comment\"># &#91;alpha]<\/span>\n<span class=\"hljs-comment\"># &#91;beta gamma]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">4) Safely handle filenames with spaces using -print0 and -0<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript language-bash\">find . -name <span class=\"hljs-string\">\"*.c\"<\/span> -print0 | xargs <span class=\"hljs-number\">-0<\/span> rm -f<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">This pattern is safer than whitespace-delimited parsing when filenames contain spaces, tabs, or newlines.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5) Use placeholder replacement with -I<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript language-bash\">printf <span class=\"hljs-string\">'%s\\n'<\/span> app.log db.log | xargs -I {} cp {} {}.bak<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">6) Legacy find cleanup patterns from the original post<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php language-bash\">find \/usr\/preserve -mtime +<span class=\"hljs-number\">30<\/span> -<span class=\"hljs-keyword\">print<\/span> | xargs rm -f\nfind \/usr\/preserve -size +<span class=\"hljs-number\">1024<\/span> -<span class=\"hljs-keyword\">print<\/span> | xargs -n <span class=\"hljs-number\">20<\/span> rm -f<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">7) Trace or confirm commands before execution<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript language-bash\">printf <span class=\"hljs-string\">'%s\\n'<\/span> file1 file2 | xargs -t rm -f\nprintf <span class=\"hljs-string\">'%s\\n'<\/span> file1 file2 | xargs -p rm -f<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">8) Run independent tasks in parallel<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript language-bash\">printf <span class=\"hljs-string\">'%s\\n'<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-number\">2<\/span> <span class=\"hljs-number\">3<\/span> <span class=\"hljs-number\">4<\/span> | xargs -n <span class=\"hljs-number\">1<\/span> -P <span class=\"hljs-number\">2<\/span> -I {} sh -c <span class=\"hljs-string\">'echo start {}; sleep 1; echo done {}'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">xargs vs find -exec: when to choose which<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Approach<\/th><th>Best for<\/th><th>Example<\/th><th>Trade-off<\/th><\/tr><\/thead><tbody><tr><td><code>xargs<\/code><\/td><td>Flexible pipelines and batching control<\/td><td><code>find . -type f -print0 | xargs -0 rm -f<\/code><\/td><td>Needs careful delimiter handling<\/td><\/tr><tr><td><code>find -exec {} +<\/code><\/td><td>Simple, safer in one command<\/td><td><code>find . -type f -name \"*.tmp\" -exec rm -f {} +<\/code><\/td><td>Less explicit control over batching style<\/td><\/tr><tr><td><code>find -exec {} \\;<\/code><\/td><td>One-file-at-a-time execution<\/td><td><code>find . -type f -exec chmod 640 {} \\;<\/code><\/td><td>Can be slower for large file sets<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Best practices and common pitfalls<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use <code>find -print0 | xargs -0<\/code> when dealing with filenames from the filesystem.<\/li><li>For destructive commands (<code>rm<\/code>, <code>mv<\/code>, <code>chmod<\/code>), start with <code>xargs -t<\/code> or replace the command with <code>echo<\/code> first.<\/li><li>Use <code>-n<\/code> to control command size and avoid oversized invocations.<\/li><li>Check your platform\u2019s <code>man xargs<\/code> for option differences, especially <code>-d<\/code> and <code>-P<\/code>.<\/li><li>When possible, prefer <code>find -exec {} +<\/code> for straightforward file operations.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Bottom line:<\/strong> <code>xargs<\/code> is a powerful command-construction tool for shell automation. Used with safe delimiters and cautious validation, it becomes a reliable building block for DevOps workflows.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide explains how to use xargs effectively in Linux\/Unix shells, from basic stdin handling to safe file operations and parallel execution. You\u2019ll learn practical command patterns, portability caveats, and safer alternatives.<\/p>\n","protected":false},"author":1,"featured_media":3174,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[4957],"tags":[378,632,460,1274,169,1271,1272,1273,1276,1275],"class_list":["post-462","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-commands","tag-examples","tag-linux","tag-linux-xargs-command","tag-unix","tag-xargs","tag-xargs-command","tag-xargs-command-examples","tag-xargs-command-line","tag-xargs-commands-tutorials"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=462"}],"version-history":[{"count":5,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/462\/revisions"}],"predecessor-version":[{"id":79347,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/462\/revisions\/79347"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/3174"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}