🐧 Linux / Shell Scripting
Intermediate
How do you use the xargs command?
Answer
xargs builds and executes commands from standard input. It takes lines or items from stdin and passes them as arguments to a command. Basic: find . -name "*.tmp" | xargs rm — deletes all .tmp files. -I {} specifies a placeholder: find . -name "*.jpg" | xargs -I {} cp {} /backup/. -P 4 runs up to 4 processes in parallel. -n 1 passes one argument per command invocation. -0 uses null byte as delimiter (pairs with find -print0) for safe handling of filenames with spaces: find . -name "*.log" -print0 | xargs -0 rm. Without -0, filenames with spaces will be split incorrectly.