brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: xargs -t prints each command before it runs

Xargs
~/tips/tip-xargs-t-trace

printf 'x\ny\n' | xargs -t touch

Shows the exact commands xargs builds, which makes a pipeline easy to check and debug.

When an xargs pipeline does something odd, the first question is: what command did it actually run? -t answers that.

printf 'x\ny\n' | xargs -t touch

Before each run, xargs writes the full command to standard error. Here it printed touch x y and then ran it. You can see right away how the input was split up, which files ended up in one command and where your placeholder went if you used -I.

Because the trace goes to stderr and the command’s own output goes to stdout, you can separate them. 2>trace.log keeps a record of everything that ran in a script, while the normal output stays clean.

I’d use -t as a first check, then remove it once you trust the line. For a dry run that runs nothing at all, put echo in front of the command instead:

find . -name '*.tmp' -print0 | xargs -0 echo rm

One thing that confuses people: -t doesn’t show quotes. A file called my file.txt appears as my file.txt in the trace, which looks like two words, even when xargs passed it correctly as one argument.

Further reading: xargs(1) manual page

More Xargs tips printf '%s\n' a b c d e f | xargs -n1 -P3 --process-slot-var=SLOT sh -c 'echo "slot $SLOT got $0"; sleep 0.2' grep -l 'TODO' *.py | xargs -o vim find . -name '*.orig' -print0 | xargs -0 -p rm find . -name '*.gz' -print0 | xargs -0 -n 1 gzip -t printf 'a b\nc d\n' | xargs -L 1 echo line: All Xargs tips (12) All topics

Link