C.W.K.
Stream
Lesson 10 of 10 · published

xargs and tee

~12 min · xargs, tee, parallel

Level 0Window Tourist
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

xargs — turn stdin into arguments

Some commands don't read stdin — rm, mv, git add. They want filenames as arguments. xargs bridges that gap: it reads stdin, splits on whitespace, and runs the next command with those tokens as arguments.

The flags you'll need

  • -n 1 — one argument per command invocation.
  • -I {} — placeholder; {} is replaced with each argument. Required when the arg isn't at the end.
  • -0 — read NUL-separated input. Pair with find -print0 for filenames containing spaces.
  • -P 4 — run up to 4 in parallel. Cheap parallelism.
  • -r — don't run the command if stdin is empty (GNU; macOS BSD xargs already does this).

tee — split the stream

tee reads stdin and writes to both stdout and one or more files. Useful when you want to save output and watch it at the same time.

  • cmd | tee out.log — write to file, also display.
  • cmd | tee -a out.log — append.
  • cmd | sudo tee /etc/protected.conf — the canonical "write a file as root from a non-root pipeline."

Putting them together

find . -name '*.log' | xargs gzip — compress every log. find . -name '*.log' -print0 | xargs -0 -I {} -P 4 gzip {} — the same, parallel and space-safe. ./build.sh | tee build.log | grep -i error — capture full output and watch errors.

Code

Find + delete safely·bash
# Print first to verify
find . -name '*.tmp' -print
# Then delete via xargs (-0 for spaces in names)
find . -name '*.tmp' -print0 | xargs -0 rm -v
Save + watch a build·bash
./build.sh 2>&1 | tee build.log | grep -i 'error\|warning'
# Append to a running log
make test 2>&1 | tee -a test.log

External links

Exercise

Make some *.tmp files: touch /tmp/{a,b,c}.tmp. Then find /tmp -name '*.tmp' -print0 | xargs -0 -I {} -P 2 rm -v {}. Note the parallel deletion. Then run seq 5 | tee count.log and confirm it both prints and writes.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.