Linux command list

xargs command: turn a list into arguments

__ __ _ _____
\ \ / /__| |_|_ _|__ _ __ _ __ ___
\ \ /\ / / _ \ '_ \| |/ _ \ '__| '_ ` _ \
\ V V / __/ |_) | | __/ | | | | | |
\_/\_/ \___|_.__/|_|\___|_| |_| |_| |_
 
A sandbox for trying xargs. Nothing here can touch your real files.
user@webterm:~/project$
 

xargs takes what comes down a pipe and hands it to the next command as arguments. Learn how to act on `find` results, when to use `-n`, and how `-I {}` places the value, by running the commands in a real terminal in your browser.

Updated: 2026-09-06

Syntax

command that lists things | xargs [options] command to run

It rearranges piped input into arguments for the next command.

Try it first

Some commands read standard input; others only take arguments. rm is the second kind.

$ find tmp -name '*.tmp'
tmp/cache1.tmp
tmp/cache2.tmp
$ find tmp -name '*.tmp' | rm
rm: missing operand

The list arrived, but rm never looked at it. With xargs in between, the list becomes arguments.

$ find tmp -name '*.tmp' | xargs rm
$ ls tmp

Gone. That is the whole job of xargs.

All at once

By default everything is handed over in one run.

$ ls logs
access.log  app.log  error.log
$ ls logs | xargs echo
access.log app.log error.log

Which makes counting the results of a find completely natural.

$ find logs -name '*.log' | xargs wc -l
3 logs/app.log
2 logs/access.log
1 logs/error.log
6 total

Paired with grep -l, which prints only the names of matching files, you can act on just the files that contain an error.

$ grep -l ERROR logs/*.log | xargs wc -l
3 logs/app.log
1 logs/error.log
4 total

One at a time

-n 1 runs the command once per value.

$ echo 'a b c' | xargs -n 1 echo
a
b
c

Choosing where the value goes

When the value must go somewhere other than the end, use -I.

$ find logs -name '*.log' | xargs -I {} cp {} backup/
$ ls backup
access.log  app.log  error.log

Each value is substituted at {}. Commands like cp, where the order of source and destination is fixed, need this form.

$ ls logs | xargs -I {} echo 'file: {}'
file: access.log
file: app.log
file: error.log

When you actually reach for it

xargs is what you type to feed what you found into what you want done.

SituationWhat to type
Delete what you foundfind . -name '*.tmp' | xargs rm
Count the lines of what you foundfind . -name '*.log' | xargs wc -l
Act only on matching filesgrep -l ERROR *.log | xargs wc -l
Copy each one somewherels *.log | xargs -I {} cp {} backup/
See what would be passedfind . -name '*.tmp' | xargs echo

Things that trip people up

Check with echo before deleting. xargs rm asks nothing. Swapping in xargs echo and reading the list first is the habit worth having.

Spaces in file names break it. xargs splits on whitespace, so my file.txt arrives as two arguments. -I {} takes the whole line as one value and avoids that.

An empty list still runs the command once.

$ find . -name 'nope*' | xargs rm
rm: missing operand

With no matches, rm runs with no arguments at all. One more reason to try destructive pipelines with xargs echo first.

Commands that read standard input do not need it. grep and sort read input themselves. Putting xargs in front turns that input into arguments, which means something different.

Practise it hands-on

webterm.appthis site

learn.webterm.appa separate site

>_WEBTERM LEARN

WebTerm Learn: from one command to actually using it

Commands stick when they show up in a real sequence of work, not one at a time. There is a course that builds them up in order.

See the course

Related commands

find / grep / ls / rm

Frequently asked questions

What is xargs for?
Commands like `rm` and `wc` read **arguments**, not standard input. `find ... | rm` does nothing useful; `find ... | xargs rm` does.
What does -I {} do?
It decides where the value goes. Each item is substituted at `{}`, which is what lets you write `cp {} backup/` with the value in the middle.
How is that different from -n 1?
`-n 1` only limits how many values go into each run; they still land at the end. `-I` is for changing where they land.
File names with spaces break it.
By default xargs splits on whitespace, so `my file.txt` arrives as two arguments. `-I {}` treats a whole line as one value, which avoids the problem.
What if nothing comes down the pipe?
The command still runs once, with no arguments. For anything destructive, run it through `xargs echo` first to see what would happen.