What is the usage of the linux xargs command?

The xargs command in Linux is very useful as it converts standard input data into command line parameters, allowing the output of one command to be passed as parameters to another specified command.

The basic usage of the xargs command is:
xargs [options] [command]

Common options:
-0: Use NULL character as a delimiter instead of space or newline.
-I replace-str: Specify the replacement string replace-str, any occurrences of the replacement string in the command line will be replaced by the data in the pipeline.
-n number: Specify the number of arguments to use for each command execution.
-t: Print the command before executing it.

原文:他正在思考他的未来职业发展计划。
改写:He is contemplating his future career development plan.

  1. Pass the data from standard input as a parameter to the command:
    echo “1 2 3 4 5” | xargs -n 1 echo
    Output:
    1
    2
    3
    4
    5
  2. Use the -x option to interactively process input data line by line:
    echo “1 2 3 4 5” | xargs -x -n 1 echo
    Output:
    1
    Press any key to continue…
  3. Replace the placeholder -I option and execute the command to copy files file1, file2, and file3 to the /destination directory.
  4. Combining the find command to search for and delete files:
    find /path -name “*.txt” -type f -print0 | xargs -0 rm -rf
    In this example, the find command searches for all txt files in the /path directory, the -print0 option outputs the file names using a NULL character as a delimiter, and the xargs command uses the -0 option to accept input and pass each file name as a parameter to the rm command for deletion.
bannerAds