Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux xargs command

Linux xargs command

Linux Command Reference Linux Command Reference

xargs (short for eXtended ARGuments) is a filter for passing arguments to commands, as well as 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 a file's output.

xargs can also convert single-line or multi-line text input into other formats, such as turning multiple lines into one line, or one line into multiple lines.

The default command for xargs is echo, which means input passed to xargs through a pipe will contain newlines and whitespace; however, after processing by xargs, newlines and whitespace are replaced with spaces.

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

The reason this command is useful is that many commands do not support passing arguments via the | pipe, yet this is often needed in daily work, hence the xargs command. For example:

find /sbin -perm +700 |ls -l       #这个命令是错误的

find /sbin -perm +700 |xargs ls -l   #这样才是正确的

xargs is generally used together with pipes.

Command format:

somecommand |xargs -item  command

Parameters:

Example

xargs acts as a replacement tool, reading input data, reformatting it, and outputting it.

Define a test file containing multiple lines of 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

Multiple lines of input, single line of 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

Multi-line output with the -n option:

# 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 define a custom delimiter:

# echo "nameXnameXnameXname" | xargs -dX



name name name name

Used together with the -n option:

# echo "nameXnameXnameXname" | xargs -dX -n2



name name

name name

Read stdin and pass the formatted arguments to a command

Suppose there is a command sk.sh and a file arg.txt that stores the arguments:

#!/bin/bash

#sk.sh command content, prints all arguments.



echo $*

Contents of arg.txt:

# cat arg.txt



aaa

bbb

ccc

An option of xargs is -I. Use -I to specify a replacement string {}; this string is replaced when xargs expands it. When -I is used with xargs, the command is executed once for each argument:

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



-p aaa -l

-p bbb -l

-p ccc -l

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

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

Using xargs with find

When using rm to delete too many files, 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 uses \0 as the delimiter.

Count the lines of 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 xargs applications

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

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

Linux Command Reference Linux Command Reference