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.
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 f4. 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 fAnswering “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 -r6. 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 D7. 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.h8. 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.h9. 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: 13107210. 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:#includeLinux 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!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
