xargs: transform output from a command into arguments for another command
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.-d <delimiter>
: specify a delimiter.
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
# run several python scripts
find . -name "myscript_??.py" | xargs -L 1 python3
Test with this online terminal:
If you have any suggestion, feel free to contact me via social media or email.
Latest tutorials and articles:
Featured content: