4 ms·
i use GNU parallel exclusively in place of xargs simply because it has --dry-run.
by rcthompson 13y ago
i use GNU parallel exclusively in place of xargs simply because it has --dry-run.
- _ZeD_ 13y agowouldn't an "echo" just do the same? $ mkdir a $ cd a $ touch b c d e $ find -type f | xargs echo rm rm ./b ./d ./e ./c $ ls b c d e $ find -type f | xargs rm $ ls $
- jasomill 13y agoOne advantage of "parallel --dry-run" over "xargs echo" is that the former quotes its output: $ touch 'Ham Jam Spam' $ touch 'J.R. "Bob" Dobbs' $ find . -type f -print0 | parallel -n1 -0 --dry-run echo echo ./Ham' 'Jam' 'Spam echo ./J.R.\ \"Bob\"\ Dobbs $ find . -type f -print0 | xargs -n1 -0 echo echo echo ./Ham Jam Spam echo ./J.R. "Bob" Dobbs For my own "dry runs", though, I've always preferred passing the command line to #include <stdio.h> int main(int argc, char* argv[]) { char** argp; int i; printf("argc = %d\n", argc); for (i = 0, argp = argv; *argp != 0; ++argp, ++i) { printf("argv[%d] = %s\n", i, *argp); } return 0; } to remove all reasonable doubt.
- lotsofcows 13y agoQuoting is nice. That's better than bash -x debug mode too.
- peterwwillis 13y agoWhy does the parallel example look weird? It put the single quotes in all the wrong places, and completely around Jam. It should have printed echo './Ham Jam Spam' but your output looks different. As an example of how it should look, try this: $ find . -type f -print0 | xargs -n1 -0 perl -e'print "\"$_\" " for (@ARGV)' "./Ham Jam Spam"
- seryoiupfurds 13y agoIt looks like it's only single-quoting the characters that need it, in his example the newlines.
- davvolun 13y agoHaving never used parallel, I still believe parallel was correct. ./Ham' 'Jam' 'Spam would be identical to ./Ham\nJam\nSpam (if \n were the correct translation to the newline in this case) or './Ham Jam Spam' This would be identical to what you wrote, but only punts to quotes when it doesn't have a canonical method of representing the character otherwise. The fact that you don't need to explicitly concatenate two strings in the shell may be what's throwing you off? Interestingly enough, 'Ham\n\nJam\nSpam' becomes ./Ham' '' 'Jam' 'Spam So parallel is just literally outputting all newlines using quotes. I believe this would be identical, if you analyzed it and saw that two newlines are next to each other: ./Ham' 'Spam' 'Jam
- rcthompson 13y agoWhat if your command has a pipe in it? Then putting echo in front won't work, because the command after the pipe is still executed. The dry run option always works, and doesn't require editing the command itself.
- dredmorbius 13y agoNice. I just had to check to see if xargs has a similar feature. It doesn't, though the --no-run-if-empty and --verbose options are both handy. I believe I've used xargs with "echo <commandlist>" to proof output before committing it. You can simply re-run that piped to bash (or your shell of choice) to execute commands if you wish (say, if parallel isn't available). E.g., echo foo bar baz | xargs -n1 -t echo ls | bash ... will execute 'ls foo; ls bar; ls baz', while showing the expanded command.
- rcthompson 13y agoYes, you can use echo, but that won't work if the command is something like "ls DIR | wc -l".
- dredmorbius 13y agoDepending on how you want to do your counts or piping, you could run that after the xargs / parallel execution, which would be much more efficient (fewer processes and execs) anyhow.