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 linux shell script xargs command

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how to use linux shell script xargs commands", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to use the linux shell script xargs command.

Xargs is a filter for passing parameters to commands and a tool for combining multiple commands. It splits a data stream into blocks small enough to facilitate filter and command processing. Typically, xargs reads data from a pipe or stdin, but it can also read data from the output of a file. The default command for xargs is echo, which means that input passed to xargs through pipes will contain line breaks and whitespace, but with xargs processing, newlines and whitespace will be replaced by spaces.

Xargs is a powerful command that captures the output of one command and passes it to another. Here are some practical examples of how to use xargs effectively.

1. When you try to 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 ~-name'* .log'- print0 | xargs-0 rm-f

two。 To get a list of all the files at the end of * .conf under / etc/, there are several different ways to get the same result. The following example is just a demonstration of how to use xargs, in which practical xargs passes the output of the find command to ls-l

# find / etc-name "* .conf" | xargs ls-l

3. 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

4. Find all the jpg files and compress it

# find /-name * .jpg-type f-print | xargs tar-cvzf images.tar.gz

5. Copy all the picture files to an external hard drive

# ls * .jpg | xargs-N1-I cp {} / external-hard-drive/directory

EXAMPLES

Find / tmp-name core-type f-print | xargs / bin/rm-f

Find files named core in or below the directory / tmp and delete them. Note that this will work incorrectly if there are any filenames containing newlines or spaces.

Find / tmp-name core-type f-print0 | xargs-0 / bin/rm-f

Find files named core in or below the directory / tmp and delete them, processing filenames in such a way that file or directory names containing spaces or newlines are correctly handled.

Find / tmp-depth-name core-type f-delete

Find files named core in or below the directory / tmp and delete them, but more efficiently than in the previous example (because we avoid the need to use fork (2) and exec (2) to launch rm and we don't need the extra xargs process).

Cut-d:-F1 < / etc/passwd | sort | xargs echo

Generates a compact listing of all the users on the system.

Xargs sh-c 'emacs "$@" < / dev/tty' emacs

Launches the minimum number of copies of Emacs needed, one after the other, to edit the files listed on xargs' standard input. This example achieves the same effect as BSD's-o option, but in a more flexible and portable way.

For example, the following command:

The code is as follows:

Rm `find / path-type f`

If there are too many files in the path directory, the error cannot be executed because the parameter list is too long. However, after switching to xargs, the problem was solved.

The code is as follows:

Find / path-type f-print0 | xargs-0 rm

In this example, xargs splits the long list of files generated by find into multiple substrings, and then calls rm on each substring. -print0 indicates that the output is separated by null (- print uses line feeds), and-0 indicates that the input is separated by null. This is much more efficient than using the find command as follows.

The code is as follows:

Find / path-type f-exec rm'{}'\

The xargs command, which should immediately follow the pipe operator, takes standard input as the main source data stream, and uses stdin to execute other commands by providing command-line arguments, such as:

The code is as follows:

Command | xargs

The example applies 1 to convert multi-line input into single-line output:

The code is as follows:

Amosli@amosli-pc:~/learn$ cat example.txt

1 2 3 4 5

6 7

eight

Amosli@amosli-pc:~/learn$ cat example.txt | xargs

1 2 3 4 5 6 7 8

For example, apply 2 to convert single-line input to multiple-line output:

The code is as follows:

Amosli@amosli-pc:~/learn$ cat example.txt | xargs-n 2

1 2

3 4

5 6

7 8

The space is the default delimiter, and-n indicates how many parameters are displayed per line

You can also use the-d parameter to separate parameters, as follows:

The code is as follows:

Amosli@amosli-pc:~/learn$ echo "splitXhiXamosliXsplit" | xargs-d "X"-n 1

Split

Hi

Amosli

Split

Instance application 3, read stdin, and pass formatting parameters to the command

The code is as follows:

# define an echo command to add # to the output parameters every time

Amosli@amosli-pc:~/learn$ cat cecho.sh

Echo $*'#

# requirement 1: output multiple parameters

Amosli@amosli-pc:~/learn$ sh cecho.sh arg1

Arg1#

Amosli@amosli-pc:~/learn$ sh cecho.sh arg2

Arg2#

Amosli@amosli-pc:~/learn$ sh cecho.sh arg3

Arg3#

# requirement 2: provide all command parameters at once

Amosli@amosli-pc:~/learn$ sh cecho.sh arg1 arg2 arg3

Arg1 arg1 arg2 arg3#

# for requirements 1 and 2, use xargs instead, and first create a new file args.txt with vi, as follows:

Amosli@amosli-pc:~/learn$ cat args.txt

Arg1

Arg2

Arg3

# batch output parameters:

Amosli@amosli-pc:~/learn$ cat args.txt | xargs-n 1

Arg1

Arg2

Arg3

Amosli@amosli-pc:~/learn$ cat args.txt | xargs-n 2 sh cecho.sh

Arg1 arg2#

Arg3#

# output all parameters at once:

Amosli@amosli-pc:~/learn$ cat args.txt | xargs sh cecho.sh

Arg1 arg2 arg3#

Requirement 3, how to embed parameters into a fixed command line? As follows:

The code is as follows:

Amosli@amosli-pc:~/learn$ sh cecho.sh-p args1-1

-p args1-cake

Amosli@amosli-pc:~/learn$ sh cecho.sh-p args2-1

-p args2-cake

Amosli@amosli-pc:~/learn$ sh cecho.sh-p args3-1

-p args3-cake

Solutions that use xargs:

The code is as follows:

Amosli@amosli-pc:~/learn$ cat args.txt | xargs-I {} sh cecho.sh-p {}-1

-p arg1-cake

-p arg2-cake

-p arg3-cake

#-I {} approved the replacement string, and the string {} will be replaced by the parameters read from stdin. When using-I, the corresponding parameters can be replaced in a loop as required.

Example application 4, use xargs with find

As an example has been given earlier, it should be noted here that the file name delimiter should be output separated by the character null, as shown below, otherwise the file may be mistakenly deleted

The code is as follows:

Amosli@amosli-pc:~/learn$ find. -type f-name "* test*.txt"-print0 | xargs-0 rm-f

Other:

The code is as follows:

Cat file | (while read arg; do cat $arg; done)

Cat file | xargs-I {} cat {}

At this point, I believe you have a deeper understanding of "how to use linux shell script xargs commands". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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