English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Linux xargs command

Linux Command大全

xargs (full name: eXtended ARGuments) is a filter for passing parameters to commands and also a tool for combining multiple commands.

xargs can convert pipe or standard input (stdin) data into command-line arguments, and can also read data from the output of a file.

xargs can also convert single-line or multi-line text input into other formats, such as converting multi-line to single-line and single-line to multi-line.

xargs' default command is echo, which means that the input passed to xargs through the pipe will contain newlines and spaces, but through the processing of xargs, newlines and spaces will be replaced with spaces.

xargs is a powerful command that can capture the output of a command and then pass it to another command.

The reason this command can be used is mainly due to the fact that many commands do not support passing parameters through the | pipe, and there is a need for this in daily work, so the xargs command was created, for example:

find /sbin -perm +700 |ls -l # This command is incorrect
find /sbin -perm +700 |xargs ls -l # This is the correct way

xargs is usually used with pipes.

Command format:

somecommand |xargs -item Command

Parameters:

  • -a file Read from the file as stdin

  • -e flag, note that it may sometimes be-E The flag must be a flag separated by spaces, and xargs stops when it analyzes a flag containing 'flag'.

  • -p Ask the user each time an argument is executed.

  • -n num The number of arguments used by the command when executed, by default using all.

  • -t means to print the command first and then execute it.

  • -i or-I, this depends on the Linux support, each item name of xargs, usually assigned line by line to {}, can use {} instead.

  • -r no-run-if-empty When xargs' input is empty, stop xargs and don't execute it anymore.

  • -s num The maximum number of characters in the command line, referring to the maximum command line character number of the command after xargs.

  • -L num Read num lines from standard input and send them to command. Command

  • -l  Same -L.

  • -d delim delimiter, the default xargs delimiter is newline, the delimiter for arguments is space, here we are modifying the delimiter of xargs.

  • -x means exit, mainly used with-s Use...

  • -P Modify the maximum number of processes, the default is1, when it is 0, it means as many as it can, this instance I didn't think of, should it be used often in daily life? I don't think so.

Online examples

xargs is used as a replacement tool, reading input data, reformatting it, and then outputting it.

Define a test file with multiline text data:

# cat test.txt
a b c d e f g
h i j k l m n
o p q
r s t
u v w x y z

Multiline input to single line output:

# cat test.txt | xargs
a b c d e f g h i j k l m n o p q r s t u v w x y z

-Usage of n option for multiline output:

# cat test.txt | xargs -n3
a b c
d e f
g h i
j k l
m n o
p q r
s t u
v w x
y z

-The d option can customize a delimiter:

# echo "nameXnameXnameXname" | xargs -dX
name name name name

Combined -Usage of n option:

# echo "nameXnameXnameXname" | xargs -dX -n2
name name
name name

Read stdin, and pass the formatted parameters to the command

Assuming a command is sk.sh and a file arg.txt to save parameters:

#!/bin/bash
# The content of sk.sh command, printing out all parameters.
echo $*

Content of arg.txt file:

# cat arg.txt
aaa
bbb
ccc

an option of xargs -I, using -I Specify a replacement string {}, which will be replaced during the expansion of xargs, when -I Used with xargs, each parameter command will be executed once:

# cat arg.txt | xargs -I {} ./sk.sh -p {} -l
-p aaa -l
-p bbb -l
-p ccc -l

Copy all image files to /data/Under the images directory:

ls *.jpg | xargs -n1 -I {} cp {} /data/images

xargs Combined with find

When you delete too many files with rm, you may get an error message:/bin/rm Argument list too long. Use xargs to avoid this problem:

find . -type f -name "*.log" -print0 | xargs -0 rm -f

xargs -0 Use \0 as delimiter.

Count the number of lines in all php files in a source code directory:

find . -type f -name "*.php" -print0 | xargs -0 wc -l

Find all jpg files and compress them:

find . -type f -name "*.jpg" -print | xargs tar -czvf images.tar.gz

Other Applications of xargs

If you have a file containing many URLs you want to download, you can use xargs to download all links:

# cat url-list.txt | xargs wget -c

Linux Command大全