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
up vote 9 down vote accepted

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
  • 2
    (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

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
one
two
three

% ls
list

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

% ls
list
one
three

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
list
one
three
two

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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