What kinds of commands can be run by xargs?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












From findutils' manual:



xargs [option...] [command [initial-arguments]]


Am I correct that xargs runs command in several run, each of which with different sequence of arguments?



If yes, does it mean that such a command must satisfy that
applying a sequence of arguments to the command is effectively the same as



  • splitting the sequence of arguments into subsequences, and


  • running the command with each subsequence of arguments in a different run?


For example, is it correct that if mycommand can be run by xargs, then



mycommand arg1 arg2 arg3


must be effectively the same as



mycommand arg1
mycommand arg2 arg3


Is the above condition a necessary and sufficient condition for a command to be executed by xargs?



Is there any command which doesn't satisfy the above condition? (I can't find any.) If there is, can it be run by xargs?



Can the arguments passed by xargs to command be any kind of arguments: options, option arguments, or nonoption arguments? I think this question is helpful for answering the above questions.



What if -i of xargs is involved?



Thanks.







share|improve this question

















  • 1




    You can't run a command with xargs that changes its semantics depending on the number of arguments, such as mv (with more than two arguments, the last argument needs to be a directory).
    – Kusalananda
    Jun 7 at 18:39











  • ... GNU mv solves this by its -t option: ... | xargs mv -t destdir
    – Kusalananda
    Jun 7 at 18:50






  • 1




    If arg2 begins with a hyphen, you may get different results in the second case if it's mistaken for an option. In general, if any of the added args start with -, you may encounter errors. It's common for the author of a script that uses xargs to make sure there's a -- at the end of initial-arguments if appropriate.
    – Mark Plotnick
    Jun 7 at 18:53











  • In addition to the issues with argument and/or option handling you seem to intend, xargs only runs a command that is actually a program, sometimes called an external command. It cannot handle a shell alias or function, or a list or a compound command like case or a special builtin like break exec eval. In contrast parallel builds commands that are run by (child) shells.
    – dave_thompson_085
    Jun 8 at 4:37














up vote
2
down vote

favorite












From findutils' manual:



xargs [option...] [command [initial-arguments]]


Am I correct that xargs runs command in several run, each of which with different sequence of arguments?



If yes, does it mean that such a command must satisfy that
applying a sequence of arguments to the command is effectively the same as



  • splitting the sequence of arguments into subsequences, and


  • running the command with each subsequence of arguments in a different run?


For example, is it correct that if mycommand can be run by xargs, then



mycommand arg1 arg2 arg3


must be effectively the same as



mycommand arg1
mycommand arg2 arg3


Is the above condition a necessary and sufficient condition for a command to be executed by xargs?



Is there any command which doesn't satisfy the above condition? (I can't find any.) If there is, can it be run by xargs?



Can the arguments passed by xargs to command be any kind of arguments: options, option arguments, or nonoption arguments? I think this question is helpful for answering the above questions.



What if -i of xargs is involved?



Thanks.







share|improve this question

















  • 1




    You can't run a command with xargs that changes its semantics depending on the number of arguments, such as mv (with more than two arguments, the last argument needs to be a directory).
    – Kusalananda
    Jun 7 at 18:39











  • ... GNU mv solves this by its -t option: ... | xargs mv -t destdir
    – Kusalananda
    Jun 7 at 18:50






  • 1




    If arg2 begins with a hyphen, you may get different results in the second case if it's mistaken for an option. In general, if any of the added args start with -, you may encounter errors. It's common for the author of a script that uses xargs to make sure there's a -- at the end of initial-arguments if appropriate.
    – Mark Plotnick
    Jun 7 at 18:53











  • In addition to the issues with argument and/or option handling you seem to intend, xargs only runs a command that is actually a program, sometimes called an external command. It cannot handle a shell alias or function, or a list or a compound command like case or a special builtin like break exec eval. In contrast parallel builds commands that are run by (child) shells.
    – dave_thompson_085
    Jun 8 at 4:37












up vote
2
down vote

favorite









up vote
2
down vote

favorite











From findutils' manual:



xargs [option...] [command [initial-arguments]]


Am I correct that xargs runs command in several run, each of which with different sequence of arguments?



If yes, does it mean that such a command must satisfy that
applying a sequence of arguments to the command is effectively the same as



  • splitting the sequence of arguments into subsequences, and


  • running the command with each subsequence of arguments in a different run?


For example, is it correct that if mycommand can be run by xargs, then



mycommand arg1 arg2 arg3


must be effectively the same as



mycommand arg1
mycommand arg2 arg3


Is the above condition a necessary and sufficient condition for a command to be executed by xargs?



Is there any command which doesn't satisfy the above condition? (I can't find any.) If there is, can it be run by xargs?



Can the arguments passed by xargs to command be any kind of arguments: options, option arguments, or nonoption arguments? I think this question is helpful for answering the above questions.



What if -i of xargs is involved?



Thanks.







share|improve this question













From findutils' manual:



xargs [option...] [command [initial-arguments]]


Am I correct that xargs runs command in several run, each of which with different sequence of arguments?



If yes, does it mean that such a command must satisfy that
applying a sequence of arguments to the command is effectively the same as



  • splitting the sequence of arguments into subsequences, and


  • running the command with each subsequence of arguments in a different run?


For example, is it correct that if mycommand can be run by xargs, then



mycommand arg1 arg2 arg3


must be effectively the same as



mycommand arg1
mycommand arg2 arg3


Is the above condition a necessary and sufficient condition for a command to be executed by xargs?



Is there any command which doesn't satisfy the above condition? (I can't find any.) If there is, can it be run by xargs?



Can the arguments passed by xargs to command be any kind of arguments: options, option arguments, or nonoption arguments? I think this question is helpful for answering the above questions.



What if -i of xargs is involved?



Thanks.









share|improve this question












share|improve this question




share|improve this question








edited Jun 7 at 18:34
























asked Jun 7 at 17:47









Tim

22.5k61222401




22.5k61222401







  • 1




    You can't run a command with xargs that changes its semantics depending on the number of arguments, such as mv (with more than two arguments, the last argument needs to be a directory).
    – Kusalananda
    Jun 7 at 18:39











  • ... GNU mv solves this by its -t option: ... | xargs mv -t destdir
    – Kusalananda
    Jun 7 at 18:50






  • 1




    If arg2 begins with a hyphen, you may get different results in the second case if it's mistaken for an option. In general, if any of the added args start with -, you may encounter errors. It's common for the author of a script that uses xargs to make sure there's a -- at the end of initial-arguments if appropriate.
    – Mark Plotnick
    Jun 7 at 18:53











  • In addition to the issues with argument and/or option handling you seem to intend, xargs only runs a command that is actually a program, sometimes called an external command. It cannot handle a shell alias or function, or a list or a compound command like case or a special builtin like break exec eval. In contrast parallel builds commands that are run by (child) shells.
    – dave_thompson_085
    Jun 8 at 4:37












  • 1




    You can't run a command with xargs that changes its semantics depending on the number of arguments, such as mv (with more than two arguments, the last argument needs to be a directory).
    – Kusalananda
    Jun 7 at 18:39











  • ... GNU mv solves this by its -t option: ... | xargs mv -t destdir
    – Kusalananda
    Jun 7 at 18:50






  • 1




    If arg2 begins with a hyphen, you may get different results in the second case if it's mistaken for an option. In general, if any of the added args start with -, you may encounter errors. It's common for the author of a script that uses xargs to make sure there's a -- at the end of initial-arguments if appropriate.
    – Mark Plotnick
    Jun 7 at 18:53











  • In addition to the issues with argument and/or option handling you seem to intend, xargs only runs a command that is actually a program, sometimes called an external command. It cannot handle a shell alias or function, or a list or a compound command like case or a special builtin like break exec eval. In contrast parallel builds commands that are run by (child) shells.
    – dave_thompson_085
    Jun 8 at 4:37







1




1




You can't run a command with xargs that changes its semantics depending on the number of arguments, such as mv (with more than two arguments, the last argument needs to be a directory).
– Kusalananda
Jun 7 at 18:39





You can't run a command with xargs that changes its semantics depending on the number of arguments, such as mv (with more than two arguments, the last argument needs to be a directory).
– Kusalananda
Jun 7 at 18:39













... GNU mv solves this by its -t option: ... | xargs mv -t destdir
– Kusalananda
Jun 7 at 18:50




... GNU mv solves this by its -t option: ... | xargs mv -t destdir
– Kusalananda
Jun 7 at 18:50




1




1




If arg2 begins with a hyphen, you may get different results in the second case if it's mistaken for an option. In general, if any of the added args start with -, you may encounter errors. It's common for the author of a script that uses xargs to make sure there's a -- at the end of initial-arguments if appropriate.
– Mark Plotnick
Jun 7 at 18:53





If arg2 begins with a hyphen, you may get different results in the second case if it's mistaken for an option. In general, if any of the added args start with -, you may encounter errors. It's common for the author of a script that uses xargs to make sure there's a -- at the end of initial-arguments if appropriate.
– Mark Plotnick
Jun 7 at 18:53













In addition to the issues with argument and/or option handling you seem to intend, xargs only runs a command that is actually a program, sometimes called an external command. It cannot handle a shell alias or function, or a list or a compound command like case or a special builtin like break exec eval. In contrast parallel builds commands that are run by (child) shells.
– dave_thompson_085
Jun 8 at 4:37




In addition to the issues with argument and/or option handling you seem to intend, xargs only runs a command that is actually a program, sometimes called an external command. It cannot handle a shell alias or function, or a list or a compound command like case or a special builtin like break exec eval. In contrast parallel builds commands that are run by (child) shells.
– dave_thompson_085
Jun 8 at 4:37










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted











Am I correct that xargs runs command in several run, each of which with different sequence of arguments?




Yeah (unless the input to xargs repeats, of course)




mycommand arg1 arg2 arg3


must be effectively the same as



mycommand arg1
mycommand arg2 arg3



Pretty much, since in general, we can't know where xargs will split the list. GNU xargs has --show-limits which could be used to determine if all input fits in one run of the command.



Of course if the arguments must be passed to the command in sets of N, we can use -n N to have some control over it (or -n M with M some multiple of N).




Is there any command which doesn't satisfy the above condition? (I can't find any.) If there is, can it be run by xargs?




Kusalananda mentioned mv, which has the issue that the last argument is treated differently from the others. In effect, the arguments added by xargs would need to go to the middle, and there's no standard way to do that. (FreeBSD xargs has -J that does it and GNU mv has the -t dir option to pass the target directory at the start of the argument list. cp is similar.)



Then something like tar -czf foo.tar.gz wouldn't work since -c (create) overwrites the archive on each run. That could be avoided by using -r (append) instead of -c, but at least GNU tar doesn't support appending to a compressed archive.



Luckily, most utilities work file-by-file and are designed to accept a list of files at the end.




Can the arguments passed by xargs to command be any kind of arguments: options, option arguments, or nonoption arguments?




They're just strings, xargs doesn't know what they mean. If you had a utility that didn't accept the usual list of files, but took a -f filename option, you could arrange for xargs to get input like -f file1 -f file2 ... and it would happily pass that along. Though with lots of input, the size limit could again cause a split between a -f and a filename, but adding something like -n 100 would make that unlikely (depending on the lengths of the filenames and the actual size limit).




What if -i of xargs is involved?




-i in GNU xargs is the same as -I , and somewhat boringly makes xargs run the command once for each input argument. Helps if you need to put the argument to the middle of the command, but doesn't really help in reducing the number of invocations to the command.






share|improve this answer





















  • Regarding that last bit, if used with -P n (if that nonstandard option is implemented to run the command in parallel), it may still be useful.
    – Kusalananda
    Jun 7 at 22:08


















up vote
1
down vote













The xargs tool reads (by default) space-delimited tokens from standard input and turns them into command-line options to the specified program. By default, it will take as many tokens as can fit on a command line and run the command once with those tokens:



echo 1 2 3 | xargs echo "-"
- 1 2 3


Here, xargs read 1, 2, and 3, and runs echo "-" 1 2 3. I'm including the "-" to help make it clear how many times echo is getting invoked.



You can limit the number of arguments that it will pass to the command using the -n option:



echo 1 2 3 | xargs -n2 echo "-"
- 1 2
- 3


Here, xargs read 2 tokens (the max) from its standard input stream, and run the command with those tokens as command-line parameters: echo "-" 1 2. Since it wasn't yet finished reading the tokens from standard input, it continued by reading 3, encountered EOF, and ran the command again with the remaining parameters: echo "-" 3.



Adjusting the value of n will give you different behavior:



echo 1 2 3 | xargs -n1 echo "-"
- 1
- 2
- 3


Edit: Tim commented that I didn't answer his questions. I disagree, and will try to explicitly call out the answers below:




Am I correct that xargs runs command in several run, each of which with different sequence of arguments?




I addressed this question. It can run command in one or several runs. By default, it try to run command as few times as possible (limited by argument length).




If yes, does it mean that such a command must satisfy that applying a sequence of arguments to the command is effectively the same as (x) splitting the sequence of arguments into subsequences, and (x) running the command with each subsequence of arguments in a different run?




I addressed this question. If the tokens are too long (or are limited by the -n option, then xargs splits them into subsequences and runs the command with the subsequences. I even give examples of that happening.




Is the above condition a necessary and sufficient condition for a command to be executed by xargs?




While I don't have an explicit answer to this, my thought was that the semantics could be inferred from the examples. If the arguments to command have special meaning based on position, then command is likely not to be a good candicate for xargs (although I'm sure you could carefully craft cases that would work).




Is there any command which doesn't satisfy the above condition? (I can't find any.) If there is, can it be run by xargs?




Again, you can carefully craft such a case (but I wouldn't recommend it). A carefully crafted example is (running mv a.txt b.txt):



$ ls
a.txt
$ echo a.txt b.txt | xargs mv
$ ls
b.txt



Can the arguments passed by xargs to command be any kind of arguments: options, option arguments, or nonoption arguments? I think this question is helpful for answering the above questions.




They are read a space-delimited tokens. They are passed to command as space-delimited arguments. This will run ls -l:



$ echo -l | xargs ls
total 0
-rw-r----- 1 user group 0 Jun 7 15:16 b.txt



What if -i of xargs is involved?




Ok, I didn't answer this one, but I don't really understand what you're asking in this context.






share|improve this answer























    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f448480%2fwhat-kinds-of-commands-can-be-run-by-xargs%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted











    Am I correct that xargs runs command in several run, each of which with different sequence of arguments?




    Yeah (unless the input to xargs repeats, of course)




    mycommand arg1 arg2 arg3


    must be effectively the same as



    mycommand arg1
    mycommand arg2 arg3



    Pretty much, since in general, we can't know where xargs will split the list. GNU xargs has --show-limits which could be used to determine if all input fits in one run of the command.



    Of course if the arguments must be passed to the command in sets of N, we can use -n N to have some control over it (or -n M with M some multiple of N).




    Is there any command which doesn't satisfy the above condition? (I can't find any.) If there is, can it be run by xargs?




    Kusalananda mentioned mv, which has the issue that the last argument is treated differently from the others. In effect, the arguments added by xargs would need to go to the middle, and there's no standard way to do that. (FreeBSD xargs has -J that does it and GNU mv has the -t dir option to pass the target directory at the start of the argument list. cp is similar.)



    Then something like tar -czf foo.tar.gz wouldn't work since -c (create) overwrites the archive on each run. That could be avoided by using -r (append) instead of -c, but at least GNU tar doesn't support appending to a compressed archive.



    Luckily, most utilities work file-by-file and are designed to accept a list of files at the end.




    Can the arguments passed by xargs to command be any kind of arguments: options, option arguments, or nonoption arguments?




    They're just strings, xargs doesn't know what they mean. If you had a utility that didn't accept the usual list of files, but took a -f filename option, you could arrange for xargs to get input like -f file1 -f file2 ... and it would happily pass that along. Though with lots of input, the size limit could again cause a split between a -f and a filename, but adding something like -n 100 would make that unlikely (depending on the lengths of the filenames and the actual size limit).




    What if -i of xargs is involved?




    -i in GNU xargs is the same as -I , and somewhat boringly makes xargs run the command once for each input argument. Helps if you need to put the argument to the middle of the command, but doesn't really help in reducing the number of invocations to the command.






    share|improve this answer





















    • Regarding that last bit, if used with -P n (if that nonstandard option is implemented to run the command in parallel), it may still be useful.
      – Kusalananda
      Jun 7 at 22:08















    up vote
    2
    down vote



    accepted











    Am I correct that xargs runs command in several run, each of which with different sequence of arguments?




    Yeah (unless the input to xargs repeats, of course)




    mycommand arg1 arg2 arg3


    must be effectively the same as



    mycommand arg1
    mycommand arg2 arg3



    Pretty much, since in general, we can't know where xargs will split the list. GNU xargs has --show-limits which could be used to determine if all input fits in one run of the command.



    Of course if the arguments must be passed to the command in sets of N, we can use -n N to have some control over it (or -n M with M some multiple of N).




    Is there any command which doesn't satisfy the above condition? (I can't find any.) If there is, can it be run by xargs?




    Kusalananda mentioned mv, which has the issue that the last argument is treated differently from the others. In effect, the arguments added by xargs would need to go to the middle, and there's no standard way to do that. (FreeBSD xargs has -J that does it and GNU mv has the -t dir option to pass the target directory at the start of the argument list. cp is similar.)



    Then something like tar -czf foo.tar.gz wouldn't work since -c (create) overwrites the archive on each run. That could be avoided by using -r (append) instead of -c, but at least GNU tar doesn't support appending to a compressed archive.



    Luckily, most utilities work file-by-file and are designed to accept a list of files at the end.




    Can the arguments passed by xargs to command be any kind of arguments: options, option arguments, or nonoption arguments?




    They're just strings, xargs doesn't know what they mean. If you had a utility that didn't accept the usual list of files, but took a -f filename option, you could arrange for xargs to get input like -f file1 -f file2 ... and it would happily pass that along. Though with lots of input, the size limit could again cause a split between a -f and a filename, but adding something like -n 100 would make that unlikely (depending on the lengths of the filenames and the actual size limit).




    What if -i of xargs is involved?




    -i in GNU xargs is the same as -I , and somewhat boringly makes xargs run the command once for each input argument. Helps if you need to put the argument to the middle of the command, but doesn't really help in reducing the number of invocations to the command.






    share|improve this answer





















    • Regarding that last bit, if used with -P n (if that nonstandard option is implemented to run the command in parallel), it may still be useful.
      – Kusalananda
      Jun 7 at 22:08













    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted







    Am I correct that xargs runs command in several run, each of which with different sequence of arguments?




    Yeah (unless the input to xargs repeats, of course)




    mycommand arg1 arg2 arg3


    must be effectively the same as



    mycommand arg1
    mycommand arg2 arg3



    Pretty much, since in general, we can't know where xargs will split the list. GNU xargs has --show-limits which could be used to determine if all input fits in one run of the command.



    Of course if the arguments must be passed to the command in sets of N, we can use -n N to have some control over it (or -n M with M some multiple of N).




    Is there any command which doesn't satisfy the above condition? (I can't find any.) If there is, can it be run by xargs?




    Kusalananda mentioned mv, which has the issue that the last argument is treated differently from the others. In effect, the arguments added by xargs would need to go to the middle, and there's no standard way to do that. (FreeBSD xargs has -J that does it and GNU mv has the -t dir option to pass the target directory at the start of the argument list. cp is similar.)



    Then something like tar -czf foo.tar.gz wouldn't work since -c (create) overwrites the archive on each run. That could be avoided by using -r (append) instead of -c, but at least GNU tar doesn't support appending to a compressed archive.



    Luckily, most utilities work file-by-file and are designed to accept a list of files at the end.




    Can the arguments passed by xargs to command be any kind of arguments: options, option arguments, or nonoption arguments?




    They're just strings, xargs doesn't know what they mean. If you had a utility that didn't accept the usual list of files, but took a -f filename option, you could arrange for xargs to get input like -f file1 -f file2 ... and it would happily pass that along. Though with lots of input, the size limit could again cause a split between a -f and a filename, but adding something like -n 100 would make that unlikely (depending on the lengths of the filenames and the actual size limit).




    What if -i of xargs is involved?




    -i in GNU xargs is the same as -I , and somewhat boringly makes xargs run the command once for each input argument. Helps if you need to put the argument to the middle of the command, but doesn't really help in reducing the number of invocations to the command.






    share|improve this answer














    Am I correct that xargs runs command in several run, each of which with different sequence of arguments?




    Yeah (unless the input to xargs repeats, of course)




    mycommand arg1 arg2 arg3


    must be effectively the same as



    mycommand arg1
    mycommand arg2 arg3



    Pretty much, since in general, we can't know where xargs will split the list. GNU xargs has --show-limits which could be used to determine if all input fits in one run of the command.



    Of course if the arguments must be passed to the command in sets of N, we can use -n N to have some control over it (or -n M with M some multiple of N).




    Is there any command which doesn't satisfy the above condition? (I can't find any.) If there is, can it be run by xargs?




    Kusalananda mentioned mv, which has the issue that the last argument is treated differently from the others. In effect, the arguments added by xargs would need to go to the middle, and there's no standard way to do that. (FreeBSD xargs has -J that does it and GNU mv has the -t dir option to pass the target directory at the start of the argument list. cp is similar.)



    Then something like tar -czf foo.tar.gz wouldn't work since -c (create) overwrites the archive on each run. That could be avoided by using -r (append) instead of -c, but at least GNU tar doesn't support appending to a compressed archive.



    Luckily, most utilities work file-by-file and are designed to accept a list of files at the end.




    Can the arguments passed by xargs to command be any kind of arguments: options, option arguments, or nonoption arguments?




    They're just strings, xargs doesn't know what they mean. If you had a utility that didn't accept the usual list of files, but took a -f filename option, you could arrange for xargs to get input like -f file1 -f file2 ... and it would happily pass that along. Though with lots of input, the size limit could again cause a split between a -f and a filename, but adding something like -n 100 would make that unlikely (depending on the lengths of the filenames and the actual size limit).




    What if -i of xargs is involved?




    -i in GNU xargs is the same as -I , and somewhat boringly makes xargs run the command once for each input argument. Helps if you need to put the argument to the middle of the command, but doesn't really help in reducing the number of invocations to the command.







    share|improve this answer













    share|improve this answer



    share|improve this answer











    answered Jun 7 at 22:04









    ilkkachu

    47.5k668131




    47.5k668131











    • Regarding that last bit, if used with -P n (if that nonstandard option is implemented to run the command in parallel), it may still be useful.
      – Kusalananda
      Jun 7 at 22:08

















    • Regarding that last bit, if used with -P n (if that nonstandard option is implemented to run the command in parallel), it may still be useful.
      – Kusalananda
      Jun 7 at 22:08
















    Regarding that last bit, if used with -P n (if that nonstandard option is implemented to run the command in parallel), it may still be useful.
    – Kusalananda
    Jun 7 at 22:08





    Regarding that last bit, if used with -P n (if that nonstandard option is implemented to run the command in parallel), it may still be useful.
    – Kusalananda
    Jun 7 at 22:08













    up vote
    1
    down vote













    The xargs tool reads (by default) space-delimited tokens from standard input and turns them into command-line options to the specified program. By default, it will take as many tokens as can fit on a command line and run the command once with those tokens:



    echo 1 2 3 | xargs echo "-"
    - 1 2 3


    Here, xargs read 1, 2, and 3, and runs echo "-" 1 2 3. I'm including the "-" to help make it clear how many times echo is getting invoked.



    You can limit the number of arguments that it will pass to the command using the -n option:



    echo 1 2 3 | xargs -n2 echo "-"
    - 1 2
    - 3


    Here, xargs read 2 tokens (the max) from its standard input stream, and run the command with those tokens as command-line parameters: echo "-" 1 2. Since it wasn't yet finished reading the tokens from standard input, it continued by reading 3, encountered EOF, and ran the command again with the remaining parameters: echo "-" 3.



    Adjusting the value of n will give you different behavior:



    echo 1 2 3 | xargs -n1 echo "-"
    - 1
    - 2
    - 3


    Edit: Tim commented that I didn't answer his questions. I disagree, and will try to explicitly call out the answers below:




    Am I correct that xargs runs command in several run, each of which with different sequence of arguments?




    I addressed this question. It can run command in one or several runs. By default, it try to run command as few times as possible (limited by argument length).




    If yes, does it mean that such a command must satisfy that applying a sequence of arguments to the command is effectively the same as (x) splitting the sequence of arguments into subsequences, and (x) running the command with each subsequence of arguments in a different run?




    I addressed this question. If the tokens are too long (or are limited by the -n option, then xargs splits them into subsequences and runs the command with the subsequences. I even give examples of that happening.




    Is the above condition a necessary and sufficient condition for a command to be executed by xargs?




    While I don't have an explicit answer to this, my thought was that the semantics could be inferred from the examples. If the arguments to command have special meaning based on position, then command is likely not to be a good candicate for xargs (although I'm sure you could carefully craft cases that would work).




    Is there any command which doesn't satisfy the above condition? (I can't find any.) If there is, can it be run by xargs?




    Again, you can carefully craft such a case (but I wouldn't recommend it). A carefully crafted example is (running mv a.txt b.txt):



    $ ls
    a.txt
    $ echo a.txt b.txt | xargs mv
    $ ls
    b.txt



    Can the arguments passed by xargs to command be any kind of arguments: options, option arguments, or nonoption arguments? I think this question is helpful for answering the above questions.




    They are read a space-delimited tokens. They are passed to command as space-delimited arguments. This will run ls -l:



    $ echo -l | xargs ls
    total 0
    -rw-r----- 1 user group 0 Jun 7 15:16 b.txt



    What if -i of xargs is involved?




    Ok, I didn't answer this one, but I don't really understand what you're asking in this context.






    share|improve this answer



























      up vote
      1
      down vote













      The xargs tool reads (by default) space-delimited tokens from standard input and turns them into command-line options to the specified program. By default, it will take as many tokens as can fit on a command line and run the command once with those tokens:



      echo 1 2 3 | xargs echo "-"
      - 1 2 3


      Here, xargs read 1, 2, and 3, and runs echo "-" 1 2 3. I'm including the "-" to help make it clear how many times echo is getting invoked.



      You can limit the number of arguments that it will pass to the command using the -n option:



      echo 1 2 3 | xargs -n2 echo "-"
      - 1 2
      - 3


      Here, xargs read 2 tokens (the max) from its standard input stream, and run the command with those tokens as command-line parameters: echo "-" 1 2. Since it wasn't yet finished reading the tokens from standard input, it continued by reading 3, encountered EOF, and ran the command again with the remaining parameters: echo "-" 3.



      Adjusting the value of n will give you different behavior:



      echo 1 2 3 | xargs -n1 echo "-"
      - 1
      - 2
      - 3


      Edit: Tim commented that I didn't answer his questions. I disagree, and will try to explicitly call out the answers below:




      Am I correct that xargs runs command in several run, each of which with different sequence of arguments?




      I addressed this question. It can run command in one or several runs. By default, it try to run command as few times as possible (limited by argument length).




      If yes, does it mean that such a command must satisfy that applying a sequence of arguments to the command is effectively the same as (x) splitting the sequence of arguments into subsequences, and (x) running the command with each subsequence of arguments in a different run?




      I addressed this question. If the tokens are too long (or are limited by the -n option, then xargs splits them into subsequences and runs the command with the subsequences. I even give examples of that happening.




      Is the above condition a necessary and sufficient condition for a command to be executed by xargs?




      While I don't have an explicit answer to this, my thought was that the semantics could be inferred from the examples. If the arguments to command have special meaning based on position, then command is likely not to be a good candicate for xargs (although I'm sure you could carefully craft cases that would work).




      Is there any command which doesn't satisfy the above condition? (I can't find any.) If there is, can it be run by xargs?




      Again, you can carefully craft such a case (but I wouldn't recommend it). A carefully crafted example is (running mv a.txt b.txt):



      $ ls
      a.txt
      $ echo a.txt b.txt | xargs mv
      $ ls
      b.txt



      Can the arguments passed by xargs to command be any kind of arguments: options, option arguments, or nonoption arguments? I think this question is helpful for answering the above questions.




      They are read a space-delimited tokens. They are passed to command as space-delimited arguments. This will run ls -l:



      $ echo -l | xargs ls
      total 0
      -rw-r----- 1 user group 0 Jun 7 15:16 b.txt



      What if -i of xargs is involved?




      Ok, I didn't answer this one, but I don't really understand what you're asking in this context.






      share|improve this answer

























        up vote
        1
        down vote










        up vote
        1
        down vote









        The xargs tool reads (by default) space-delimited tokens from standard input and turns them into command-line options to the specified program. By default, it will take as many tokens as can fit on a command line and run the command once with those tokens:



        echo 1 2 3 | xargs echo "-"
        - 1 2 3


        Here, xargs read 1, 2, and 3, and runs echo "-" 1 2 3. I'm including the "-" to help make it clear how many times echo is getting invoked.



        You can limit the number of arguments that it will pass to the command using the -n option:



        echo 1 2 3 | xargs -n2 echo "-"
        - 1 2
        - 3


        Here, xargs read 2 tokens (the max) from its standard input stream, and run the command with those tokens as command-line parameters: echo "-" 1 2. Since it wasn't yet finished reading the tokens from standard input, it continued by reading 3, encountered EOF, and ran the command again with the remaining parameters: echo "-" 3.



        Adjusting the value of n will give you different behavior:



        echo 1 2 3 | xargs -n1 echo "-"
        - 1
        - 2
        - 3


        Edit: Tim commented that I didn't answer his questions. I disagree, and will try to explicitly call out the answers below:




        Am I correct that xargs runs command in several run, each of which with different sequence of arguments?




        I addressed this question. It can run command in one or several runs. By default, it try to run command as few times as possible (limited by argument length).




        If yes, does it mean that such a command must satisfy that applying a sequence of arguments to the command is effectively the same as (x) splitting the sequence of arguments into subsequences, and (x) running the command with each subsequence of arguments in a different run?




        I addressed this question. If the tokens are too long (or are limited by the -n option, then xargs splits them into subsequences and runs the command with the subsequences. I even give examples of that happening.




        Is the above condition a necessary and sufficient condition for a command to be executed by xargs?




        While I don't have an explicit answer to this, my thought was that the semantics could be inferred from the examples. If the arguments to command have special meaning based on position, then command is likely not to be a good candicate for xargs (although I'm sure you could carefully craft cases that would work).




        Is there any command which doesn't satisfy the above condition? (I can't find any.) If there is, can it be run by xargs?




        Again, you can carefully craft such a case (but I wouldn't recommend it). A carefully crafted example is (running mv a.txt b.txt):



        $ ls
        a.txt
        $ echo a.txt b.txt | xargs mv
        $ ls
        b.txt



        Can the arguments passed by xargs to command be any kind of arguments: options, option arguments, or nonoption arguments? I think this question is helpful for answering the above questions.




        They are read a space-delimited tokens. They are passed to command as space-delimited arguments. This will run ls -l:



        $ echo -l | xargs ls
        total 0
        -rw-r----- 1 user group 0 Jun 7 15:16 b.txt



        What if -i of xargs is involved?




        Ok, I didn't answer this one, but I don't really understand what you're asking in this context.






        share|improve this answer















        The xargs tool reads (by default) space-delimited tokens from standard input and turns them into command-line options to the specified program. By default, it will take as many tokens as can fit on a command line and run the command once with those tokens:



        echo 1 2 3 | xargs echo "-"
        - 1 2 3


        Here, xargs read 1, 2, and 3, and runs echo "-" 1 2 3. I'm including the "-" to help make it clear how many times echo is getting invoked.



        You can limit the number of arguments that it will pass to the command using the -n option:



        echo 1 2 3 | xargs -n2 echo "-"
        - 1 2
        - 3


        Here, xargs read 2 tokens (the max) from its standard input stream, and run the command with those tokens as command-line parameters: echo "-" 1 2. Since it wasn't yet finished reading the tokens from standard input, it continued by reading 3, encountered EOF, and ran the command again with the remaining parameters: echo "-" 3.



        Adjusting the value of n will give you different behavior:



        echo 1 2 3 | xargs -n1 echo "-"
        - 1
        - 2
        - 3


        Edit: Tim commented that I didn't answer his questions. I disagree, and will try to explicitly call out the answers below:




        Am I correct that xargs runs command in several run, each of which with different sequence of arguments?




        I addressed this question. It can run command in one or several runs. By default, it try to run command as few times as possible (limited by argument length).




        If yes, does it mean that such a command must satisfy that applying a sequence of arguments to the command is effectively the same as (x) splitting the sequence of arguments into subsequences, and (x) running the command with each subsequence of arguments in a different run?




        I addressed this question. If the tokens are too long (or are limited by the -n option, then xargs splits them into subsequences and runs the command with the subsequences. I even give examples of that happening.




        Is the above condition a necessary and sufficient condition for a command to be executed by xargs?




        While I don't have an explicit answer to this, my thought was that the semantics could be inferred from the examples. If the arguments to command have special meaning based on position, then command is likely not to be a good candicate for xargs (although I'm sure you could carefully craft cases that would work).




        Is there any command which doesn't satisfy the above condition? (I can't find any.) If there is, can it be run by xargs?




        Again, you can carefully craft such a case (but I wouldn't recommend it). A carefully crafted example is (running mv a.txt b.txt):



        $ ls
        a.txt
        $ echo a.txt b.txt | xargs mv
        $ ls
        b.txt



        Can the arguments passed by xargs to command be any kind of arguments: options, option arguments, or nonoption arguments? I think this question is helpful for answering the above questions.




        They are read a space-delimited tokens. They are passed to command as space-delimited arguments. This will run ls -l:



        $ echo -l | xargs ls
        total 0
        -rw-r----- 1 user group 0 Jun 7 15:16 b.txt



        What if -i of xargs is involved?




        Ok, I didn't answer this one, but I don't really understand what you're asking in this context.







        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Jun 7 at 19:23


























        answered Jun 7 at 18:03









        Andy Dalton

        4,7561520




        4,7561520






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f448480%2fwhat-kinds-of-commands-can-be-run-by-xargs%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)