xargs – possible uses

Xargs is one of those commands I have never used. Recently I was looking into how I can use them in the scripts I use on a daily basis.

Here are some. These are for personal references. If you find it useful. Please go ahead and use them.

  1. xargs, was actually used to get over the limitations of the number of arguments that a certain command can handle. But then it has been used for other purposes as well now I guess.
  2. ls somefilepattern | xargs can convert the list that would have been displayed vertically into a list that is displayed on a single line seperated by spaces, since it is actually converting it to an argument list for some command. This can be a really useful oneliner.
  3. When you want it to act as an argument list for some command then ls somefilepattern | xargs command will do. What this is doing is that it is taking a list of those files, converting them into an argument list and passing it to the command. Good!
  4. A good derivation of the last two points is that it can be used with commands like the cut command to get some vertical list, which can then be passed on to a command or just convert it to a oneline list. Ex: ls * | cut -d "." -f1 | xargs

They say it is mostly used with find command but then I cannot relate to those uses.