Operations 7 min read

Mastering xargs: Powerful Techniques for Linux Command-Line Automation

This guide explains how the xargs utility transforms piped or stdin data into command arguments, covering its syntax, options for limiting arguments, running multiple commands, custom delimiters, reading from files, and combining with find for efficient batch operations on Linux systems.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering xargs: Powerful Techniques for Linux Command-Line Automation

What is xargs?

xargs is a filter that builds and executes command lines from standard input or a pipe, defaulting to echo. It replaces newline characters with spaces, allowing commands that do not natively accept piped input to receive arguments.

Basic Syntax

xargs [OPTIONS] [COMMAND [initial-arguments]]

Running a Simple Command

Example: create three files by piping a space‑separated list to xargs with the -t flag to print the command before execution.

# echo "file1 file2 file3" | xargs -t touch
touch file1 file2 file3

Limiting the Number of Arguments

The -n option specifies how many arguments to pass to each command invocation. xargs will run the command repeatedly until all arguments are consumed.

# echo "file1 file2 file3" | xargs -n1 -t touch
touch file1
touch file2
touch file3

Running Multiple Commands

Use -i or -I to replace a placeholder with each argument, allowing multiple commands per argument.

# echo "file1 file2 file3" | xargs -t -I % sh -c 'touch %; ls -l %'
sh -c touch file1 file2 file3; ls -l file1 file2 file3
-rw-r--r--. 1 root root 0 Jan 30 00:18 file1
-rw-r--r--. 1 root root 0 Jan 30 00:18 file2
-rw-r--r--. 1 root root 0 Jan 30 00:18 file3

Specifying a Custom Delimiter

Use -d or --delimiter to define a single‑character delimiter, which can be an escaped character such as \.

# echo -n "file1#file2#file3#file4" | xargs -d \# -t touch
touch file1 file2 file3 file4

Reading Arguments from a File

The -a option tells xargs to read arguments from a file instead of stdin. Combine with -L 1 to process one line at a time.

# cat ip.txt
114.114.114.114
www.linuxprobe.com
202.102.128.68
# xargs -a ip.txt -t -L 1 ping -c 1
ping -c 1 114.114.114.114
... (output omitted for brevity) ...
ping -c 1 202.102.128.68
... (output omitted for brevity) ...

Combining xargs with find

When dealing with filenames that may contain spaces or newlines, use find -print0 together with xargs -0 (or --null) to safely pass the list to another command, such as creating a tar archive.

# find log/ -type f -print0 | xargs --null tar -zcvf logs.tar.gz
log/anaconda/anaconda.log
log/anaconda/syslog
... (additional files) ...

Summary

xargs bridges the gap for commands that cannot directly accept piped input, offering flexible argument handling, batch execution, and seamless integration with other utilities like find. Its various options make it a powerful tool for Linux automation and system administration.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

SysadminShell scriptingxargs
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.