Quick start of xargs command to execute a command from another command with their arguments piped.
July 3, 2024 · 1 min · Elvin Guti
xargs is a great command that can help us to run commands from the output of another command.
From tldr xargs:
Execute a command with piped arguments coming from another command, a file, etc. The input is treated
as a single block of text and split into separate pieces on spaces, tabs, newlines and end-of-file.
For the examples we’re going to use the seq command:
# Print 3 numbersseq 3# 1# 2# 3# xorgs wil print the numbers "inline"seq 3| xargs
# 1 2 3# -n will use a max number of arguments per command lineseq 3| xargs -n 2# 1 2# 3# -I is used to replace {} with the argument from the previous commandseq 3| xargs -I {}echo'number {} printed'# number 1 printed# number 2 printed# number 3 printed# Create 1, 2, 3 foldersseq 3| xargs mkdir
# Remove 1, 2, 3 foldersseq 3| xargs rm -rf