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

Introduction to the usage of Linux xargs command

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces the relevant knowledge of "introduction to the usage of Linux xargs command". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Xargs is a filter for passing parameters to commands and a tool for combining multiple commands.

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

Xargs can also convert single-line or multi-line text input to other formats, such as multi-line to single-line, single-line to multiline.

The default command for xargs is echo, which means that input passed to xargs through the pipeline will contain line breaks and whitespace, but with xargs processing, line feeds and whitespace will be replaced by spaces.

Xargs is a powerful command that captures the output of one command and passes it to another.

The key reason why this command can be used is that many commands do not support | pipes to pass parameters, which is necessary in daily work, so there is a xargs command, such as

Find / sbin-perm + 700 | ls-l # this command is incorrect

Find / sbin-perm + 700 | xargs ls-l # this is the right way

Xargs is usually used with pipes.

Command format:

Somecommand | xargs-item command

Parameters:

-a file is read from the file as sdtin

-e flag, note that sometimes it may be-E flag flag must be a space-separated flag, which stops when the xargs parses the flag containing the flag.

-p ask the user once when one argument is executed at a time.

-n num is followed by a number of times to indicate the number of argument used at a time when the command is executed. By default, all are used.

-t means to print the command before executing it.

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

-r no-run-if-empty stops xargs when the input to xargs is empty, and you don't have to execute it anymore.

The maximum number of characters on the-s num command line, which refers to the maximum number of command line characters for the command that follows xargs.

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

-l is the same as-L.

The-d delim delimiter. The default xargs delimiter is carriage return, and the argument delimiter is a space. Here, the xargs delimiter is modified.

The meaning of-x exit is mainly used with-s.

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

Example:

Xargs is used as a replacement tool to read the amount of input data and reformat the output.

Define a test file:

[root@127-0-0-1 hank] # cat 3.sql

1 2 3 4 5

A b c de f

Ooo

Multi-line input single-line output:

[root@127-0-0-1 hank] # cat 3.sql | xargs

1 2 3 4 5 a b c de f ooo

-n option multi-line output:

[root@127-0-0-1 hank] # cat 3.sql | xargs-n 3

1 2 3

4 5 a

B c de

F ooo

The-d option allows you to customize a delimiter:

[root@127-0-0-1 hank] # echo "redisXredisXredisXredis" | xargs-d X

Redis redis redis redis

Use in conjunction with the-n option:

[root@127-0-0-1 hank] # echo "redisXredisXredisXredis" | xargs-d X-n 2

Redis redis

Redis redis

Read the stdin and pass the formatted parameters to the command

Suppose a command is sk.sh and a file hank.txt that saves parameters:

#! / bin/bash

# sk.sh command content, print out all parameters.

Echo $*

Contents of hank.txt file:

[root@127-0-0-1 hank] # cat hank.txt

Aaa

Bbb

Ccc

An option of xargs-I, use-I to specify a replacement string {}, which is replaced when xargs is extended. When-I is used in conjunction with xargs, each parameter command is executed once:

[root@127-0-0-1 hank] # cat hank.txt | xargs-I {}. / sk.sh-get-redis {}-end

-get-redis aaa-end

-get-redis bbb-end

-get-redis ccc-end

Copy all files to the / u01/hank/yoon directory:

[root@127-0-0-1 hank] # ls

1.sql 2.sql 3.sql hank.txt sk.sh yoon

[root@127-0-0-1 hank] # find / u01/hank/-type f-name "* .sql" | xargs-I {} cp {} yoon/

[root@127-0-0-1 hank] # cd yoon/

[root@127-0-0-1 yoon] # ls

1.sql 2.sql 3.sql

Xargs is used in conjunction 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:

[root@127-0-0-1 hank] # find / u01/hank/-type f-name "* .sql"

/ u01/hank/1.sql

/ u01/hank/2.sql

/ u01/hank/3.sql

[root@127-0-0-1 hank] # find / u01/hank/-type f-name "* .sql"-print0

/ u01/hank/1.sql/u01/hank/2.sql/u01/hank/3.sql

[root@127-0-0-1 hank] # find / u01/hank/-type f-name "* .sql"-print0 | xargs-0

/ u01/hank/1.sql / u01/hank/2.sql / u01/hank/3.sql

[root@127-0-0-1 hank] # find / u01/hank/-type f-name "* .sql"-print0 | xargs-0 rm-f

Xargs-0 uses\ 0 as the delimiter

Delete all files except the 1.sql 2.sql file:

Find / u01/hank/-type f!-name "1.sql"-and-type f!-name "2.sql" | xargs rm-f

Use the grep command to search for the word omc in all ordinary files in the current directory:

Find / etc/-name\ *-type f | xargs grep "omc" > / tmp/ftl or

> find / etc/-name "*"-type f | xargs grep "omc" > / tmp/ftl

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

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

Find all the 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 that contains a lot of URL you want to download, you can use xargs to download all the links:

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

This is the end of the introduction to the usage of the Linux xargs command. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Servers

Wechat

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

12
Report