Introducing xargs working principle and common usage

Working Principle

xargs reads data from standard input stdin, and splits according to delimiters (usually spaces, tabs, or newlines), passes the split parts as parameters to the following command. Then repeats continuously. Steps as follows:

  1. Read stdin, detect delimiter
  2. Build command, execute
  3. Wait for command execution to complete, then repeat step 1

Common Usage

xargs is commonly used when needing to pass one command’s output as another command’s parameters. Compare | data flow as follows:

  • | stdout -> stdin: Pipe can make standard output to standard input
  • xargs stdin -> command arguments: Can make standard input to command line arguments
  • command1 | xargs command2 stdout -> stdin -> command arguments Combined use can make command 1 output to command 2 parameters

When xargs doesn’t use any options, it will split out one part according to each delimiter, then execute command once, is a loop execution process

# Delete multiple files, ls shows all files, filter txt format, then each file as rm parameter, loop delete
ls | grep .txt | xargs rm

When building complex commands, can use -I option, -I followed by placeholder, each replacement, the split fragment will replace placeholder to build command

ls | grep .txt | xargs -I {} cp {} /destination/

Can also use -P option for concurrent command execution

cat file.txt | xargs -P 4 -I {} command {}

How to debug xargs?

Use -p option can enter prompt mode, will prompt user before each command execution, this way can see each executed command

References