Fundamentals 10 min read

Mastering xargs: Powerful Linux Command‑Line Tricks Explained

This tutorial walks through the fundamentals of the Linux xargs command, showing how to combine it with other tools, use various options like -d, -n, -p, -t, and integrate it with find, grep, and rm to process input efficiently and safely.

Linux Cloud Computing Practice
Linux Cloud Computing Practice
Linux Cloud Computing Practice
Mastering xargs: Powerful Linux Command‑Line Tricks Explained

When combined with other commands, the xargs utility becomes extremely useful. This tutorial explains its usage through several simple examples.

xargs [-0prtx] [-E eof-str] [-e[eof-str]] [--eof[=eof-str]] [--null] [-d delimiter] [--delimiter delimiter] [-I replace-str] [-i[replace-str]] [--replace[=replace-str]] [-l[max-lines]] [-L max-lines] [--max-lines[=max-lines]] [-n max-args] [--max-args=max-args] [-s max-chars] [--max-chars=max-chars] [-P max-procs] [--max-procs=max-procs] [--interactive] [--verbose] [--exit] [--no-run-if-empty] [--arg-file=file] [--show-limits] [--version] [--help] [command [initial-arguments]]

1. Basic xargs example

The xargs command reads from standard input and, by default, runs /bin/echo on the input.

$ xargs
Hi,
Welcome to TGS.

After typing the input and pressing Ctrl+D , the string is echoed back:

$ xargs
Hi,
Welcome to TGS.
Hi, Welcome to TGS.

2. Using the -d option to specify a delimiter

The -d option lets you treat each character literally as a delimiter. Using -d\n preserves newline characters in the output.

$ xargs -d

Hi,
Welcome to TGS.
Hi,
Welcome to TGS.

3. Limiting output lines with -n

The -n option splits the output into multiple lines, showing a specified number of items per line.

$ echo a b c d e f | xargs -n 3
a b c
d e f
$ echo a b c d e f | xargs -n 2
a b
c d
e f

4. Prompting the user before execution with -p

The -p option asks for confirmation before running each command.

$ echo a b c d e f | xargs -p -n 3
/bin/echo a b c ?
/bin/echo d e f ?
y
d e f

Answering “n” prevents execution.

5. Avoiding default /bin/echo on empty input with -r

When there is no input, xargs normally runs /bin/echo. Using -r suppresses this behavior.

$ xargs -r

6. Printing the command and its output with -t

The -t option displays the command that xargs executes before showing the output.

$ xargs -t
A B C D
/bin/echo A B C D
A B C D

7. Combining xargs with find

This is one of the most important uses: feed the list of files from find into xargs to run another command, such as rm -rf.

$ ls
one.c  one.h  two.c  two.h
$ find . -name "*.c" | xargs rm -rf
$ ls
one.h  two.h

8. Deleting files with spaces in their names

Files whose names contain spaces are not removed by the simple find | xargs rm pipeline. Using -print0 with find and -0 with xargs handles such names correctly.

$ touch "The Geek Stuff.c"
$ find . -name "*.c" -print0 | xargs -0 rm -rf
$ ls
one.h  two.h

9. Showing system limits with --show-limits

$ xargs --show-limits
Your environment variables take up 1203 bytes
POSIX upper limit on argument length: 2093901
POSIX smallest allowable upper limit on argument length: 4096
Maximum length of command we could actually use: 2092698
Size of command buffer we are actually using: 131072

10. Combining xargs with grep

Use xargs to pass the list of files from find to grep for pattern searching.

$ find . -name '*.c' | xargs grep 'stdlib.h'
./tgsthreads.c:#include
./valgrind.c:#include
./direntry.c:#include
./xvirus.c:#include
./temp.c:#include
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.

Unixcommand-linexargs
Linux Cloud Computing Practice
Written by

Linux Cloud Computing Practice

Welcome to Linux Cloud Computing Practice. We offer high-quality articles on Linux, cloud computing, DevOps, networking and related topics. Dive in and start your Linux cloud computing journey!

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.