xargs allows you to build command lines from standard input, reading items separated by blanks or newlines.

Parameters:

  • -0: items are separated by a null, not whitespace. Useful when you need to pass text with spaces as one argument.
  • -L <number>: limit the number of arguments per command line.
  • -I <word>: replace <word> with standard input.
  • -i: similar to -I, but use {} as a placeholder for input.

These are some examples:

# list contents of all folders from / (not recursive)
find / -type d -maxdepth 1 | xargs ls
# download files from a list of URLs
cat urls.txt | xargs wget
# get basenames of a files list
# -L limits the number of arguments per command line
find . -type f | xargs -L 1 basename
# specify where to add the output of the first command
echo 2 | xargs -I mynumber echo 1 mynumber 3
# same as:
echo 2 | xargs -i echo 1 {} 3
Test with this online terminal: