🐧 Linux / Shell Scripting
Intermediate
How does xargs work for building command lines?
Answer
xargs reads items from stdin (typically newline or null-separated) and builds and executes command lines from them. Basic usage: find . -name "*.tmp" | xargs rm deletes all .tmp files found. xargs -n 2 passes at most 2 arguments per command invocation. xargs -P 4 runs up to 4 processes in parallel — great for speeding up operations on many files. xargs -I{} cmd {} extra uses a placeholder for precise argument placement: find . -name "*.jpg" | xargs -I{} convert {} {}.png. Always use -print0 with find and -0 with xargs to correctly handle filenames containing spaces or special characters: find . -name "*.log" -print0 | xargs -0 rm.