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 -p asks for a yes before each command it runs

Xargs
~/tips/tip-xargs-p-prompt

find . -name '*.orig' -print0 | xargs -0 -p rm

Shows every command xargs is about to run and waits for you to confirm it.

Piping a list of files into rm always makes me a little nervous. With -p, xargs shows each command and waits for an answer.

find . -name '*.orig' -print0 | xargs -0 -p rm

xargs prints the full command, something like rm ./a.orig ./b.orig ?..., and only runs it when you type y or Y. Anything else skips that command and moves on to the next one. -p also switches on -t, so you see what’s being run.

Because xargs packs many files into one command, you may get a single question for all of them. To decide per file, add -n 1:

find . -name '*.orig' -print0 | xargs -0 -n 1 -p rm

The answer is read from the terminal, not from standard input, because standard input is where the file list comes from. That has a side effect: in a cron job or any script without a terminal, -p doesn’t work. Without a terminal, for example in a container, xargs says it can’t open /dev/tty and nothing is deleted, which is at least a safe way to fail.

For one-off cleanups at the prompt, I find it a good middle ground between running blind and checking every name by hand.

Further reading: xargs(1) manual page, GNU findutils manual: Invoking xargs

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 '*.gz' -print0 | xargs -0 -n 1 gzip -t printf 'a b\nc d\n' | xargs -L 1 echo line: printf 'x\ny\n' | xargs -t touch All Xargs tips (12) All topics

Link