A fair number of linux commands have a dry-run option that will show you what they're going to do without doing it. I see nothing in the xargs man page that does that and no obvious way to emulate it.

(my specific use case is troubleshooting long pipelines, though I'm sure there are others)

Am I missing something?

  • what exactly you are trying to do ?
    – Rahul
    Aug 3 '16 at 17:52
  • If you do want to execute + print each command in advance, use xargs -n1 --verbose.
    – Noam Manos
    May 20 '20 at 7:31

You may benefit from the -p or -t flags.

xargs -p or xargs --interactive will print out the command to be executed and then prompt for input (y/n) to confirm before executing the command.

% cat list

% ls

% cat list | xargs -p -I {} touch {}
touch one ?...y
touch two ?...n
touch three ?...y

% ls

xargs -t or xargs --verbose will print each command, then immediately execute it:

% cat list | xargs -t -I {} touch {}
touch one 
touch two 
touch three 

% ls
  • 7
    superb answer with both -p and -t options. excellent examples. thank you!
    – JCotton
    Dec 22 '18 at 4:41
  • On mac BSD xargs the --verbose option isn't recognized, but -t works!
    – Carl Walsh
    Apr 1 '21 at 14:54
  • Agreed - Great answer. One tweak I might suggest, though. While it's good to know the -I {} option for more complicated cases, it probably over-complicates that example slightly. More concise would be to use -L 1 as in cat list | xargs -L 1 -p touch, right? Apr 6 '21 at 16:03

Put an echo in front of the command to run?

$ echo a b c d e | xargs -n2 echo rm
rm a b
rm c d
rm e
  • 7
    (This, is of course only a simple solution that works in trivial cases. The output will be ambiguous if the parameters contain spaces or control characters. We'd need some dedicated tool to unambiguously print the parameters it receives.)
    – ilkkachu
    Aug 4 '16 at 12:14
  • What about if you're trying to dry-run echo? :)
    – mbigras
    Apr 10 '17 at 0:19
  • This isn't the answer to the question, and it shouldn't be the accepted one. This answer show an example of using 'xargs' on the 'echo' command. The OP wants to run 'xargs' on an arbitrary command, but also show the computed command which is about to be fired. The correct answer is below, by Kevin Hencke, about the -t and -p flags.
    – Gabriel
    Apr 27 '20 at 9:24
  • @Gabriel, of course it's an answer to the question. It's not a complete answer, of course, more like an idea (which is somewhat hinted at by the question mark, and the comment noting that there are limitations). But an answer it is, and it's up to every one else to decide how to vote on it. Not that there seem to have been many alternatives in, oh, one and a half years.
    – ilkkachu
    Apr 27 '20 at 17:27
  • @Gabriel, note that the question mentioned "[an] option that will show you what they're going to do without doing it." -- I can't really see how --verbose and --interactive could be bent to do that, unless someone can figure out a way to pipe the output of yes n to xargs --interactive (I tried and failed). Also, --verbose also has the issue that its output is ambiguous, e.g. echo '"a b" c' | xargs --verbose rm prints rm a b c, the same as echo 'a b c' | xargs --verbose rm prints, even though the commands are very much not the same.
    – ilkkachu
    Apr 27 '20 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.