Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the xargs command in Linux

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly describes how to use the xargs command in Linux, the article is very detailed, has a certain reference value, interested friends must read it!

On Linux, the find command passes the matched file to xargs, which fetches only a portion of the file at a time rather than all of it, and xargs processes files that are not at the end with the-i argument.

Command Format:

somecommand |xargs -item command Parameters:

-a file read from file as sdtin

-e flag, note that sometimes it may be-E, flag must be a flag separated by spaces, and stop when xargs analyzes to contain flag.

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

-n num followed by the number of times, indicating the number of arguments used at one time when the command is executed, the default is to use all.

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

-i or-I, depending on linux support, assign each name of xargs, usually one line at a time, to {}, which can be replaced by {}.

-r no-run-if-empty Stop xargs when the input to xargs is empty.

-s num Maximum number of characters on the command line, which refers to the maximum number of characters on the command line following xargs.

-L num reads num lines once from standard input and sends them to command.

-l with-L.

-d delim delimiter, default xargs delimiter is carriage return, argument delimiter is space, modified here is xargs delimiter.

-x exit means, mainly used with-s.

-P modifies the maximum number of processes. The default is 1. When it is 0, it is as many as it can. I didn't think of this example. It should not be used at ordinary times.

Instance xargs is used as a replacement tool, reading input data and reformatting it for output.

Define a test file with 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 line input 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-n option multi-line input:

# 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-d option You can customize a delimiter:

# echo "nameXnameXnameXname" |xargs -dX name in combination with the-n option:

# echo "nameXnameXnameXname" |xargs -dX -n2 name reads stdin and passes formatted arguments to the command

Suppose a command is sk.sh and a file arg.txt holds parameters:

#!/ bin/bash #sk.sh command content, print out all parameters. echo $* arg.txt File content:

# cat arg.txt aaa bbb cccxargs an option-I, use-I to specify a replacement string {}, this string will be replaced when xargs expansion, when-I combined 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/images directory:

ls *.jpg |xargs -n1 -I {} cp {} /data/imagesxargs in conjunction with find

When deleting 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 -fxargs -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.gzxargs Other Apps

If you have a file that contains many URLs you wish to download, you can download all the links using xargs:

# cat url-list.txt |xargs wget -c Above is "How to use xargs command in Linux" All the contents of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report