JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Linux parameter substitution command xargs

Author:JIYIK Last Updated:2025/04/07 Views:

The xargs command is also a member of the pipeline command. The function of the xargs command is simply to replace parameters. So what is parameter replacement? First, we need to understand the concept of pipelines. In the section on Linux pipeline commands, we introduced the concept of pipeline commands in detail. Here we will just briefly explain it. First, let's take a look at the flowchart of pipeline commands.

Pipeline command execution flow chart
Pipeline command execution flow chart

That is, the output of the command in front of the pipeline is used as the standard input of the following command. Since it is the standard input, there is a requirement that the following command can read data from the standard input. This condition is still quite demanding. In Linux, not all commands can read data from the standard input device. There are not many commands that can read data. In addition to the cut, grep, wc and other commands introduced earlier, xargs is also a command that can read data from the standard input. xargs reads data from the standard input, and then uses this data as the input for commands that cannot read data from the standard input. So the xargs command, to put it bluntly, is used to transfer data.

Let's take a simple example to see the role of the xargs command

$ echo php | ls

The above example is to use ls to display the contents of the php directory, but unfortunately, ls is not a pipeline command and cannot read data from the standard input device, so it will not display the contents of the php directory. Because we are currently in the workspace directory, the above command will display the contents of the workspace directory.

However, if we use the xargs command to transfer data, the result will be different.

$ echo php | xargs ls

In my system, the contents of the php directory are as follows (the contents of the php directory can be listed)

xargs uses ls to display directories


xargs Command Syntax

From the above example, we can see that the syntax of the xargs command is as follows

$ xargs [-options] [command]
The default command after xargs is echo.

Most of the time, the xargs command is used with pipes, but it can also be used alone.

After inputting xargs and pressing Enter, the terminal starts waiting for user input and takes the user input as standard input. We can input any content and then press ctrl+d to end the input. This is the echo command that will display the content we input.

$ xargs find -name
*.txt
# 然后按 ctrl+d 结束输入会显示
jiyik.txt

Example of xargs parameters

From the above, we have a simple understanding of the syntax of the xargs command and its basic usage. Next, we will give examples of the parameters of the xargs command one by one

-d parameter and separator

By default, xargs uses spaces as delimiters to split the data in the standard input device and generate command parameters one by one, for example

$ echo "dir1 dir2 dir3" | xargs mkdir

This will generate three folders in the current directory

dir1 dir2 dir3

If you can customize the delimiter, use a colon as the delimiter as follows

$ echo "dir1:dir2:dir3" | xargs -d ":" mkdir

Three folders dir1 dir2 and dir3 will also be generated. If the -d option is not used to specify the separator, only one folder dir1:dir2:dir3 will be generated.

-p confirms the command to be executed

After using the xargs command, the executed command must be confirmed due to the parameter conversion process.

The -p parameter is used to confirm the command to be executed

$ echo "dir1 dir2 dir3" | xargs -p mkdir

After execution, the command to be executed will be displayed in the terminal, and then the user will be asked to confirm. Enter y (uppercase or lowercase) to continue executing the command.

xargs command -p parameter execution results

-t Display the command to be executed

The -t option is used to display the command to be executed. Unlike the -p option, it does not require user confirmation.

$ echo "dir1 dir2 dir3" | xargs -t mkdir

Will directly display

mkdir dir1 dir2 dir3

-L specifies the number of lines

If the standard input contains multiple lines, -L can specify how many lines to use as the next command parameter, such as the following example

$ xargs find -name
*.txt
*.md
find: 路径必须在表达式之前: *.md

The above command uses two lines of data as find parameters at the same time, resulting in an error.

So in this case we can use the -L option to specify each line as a parameter, so that no error is reported

$ xargs -L 1 find -name
*txt
jiyik.txt
*md
jiyik.md

-0 with find command

Since xargs uses spaces as delimiters by default, it is not very suitable for handling file names, which may contain spaces.

The find command has a special parameter -print0, which specifies that the output file list is separated by null. Then, the -0 parameter of the xargs command means using null as a separator.

$ find /path -type f -print0 | xargs -0 rm

The above command deletes all files in the /path path. Since the separator is null, no error is reported even if the file name contains spaces.

There is another reason why xargs is particularly suitable for the find command. Some commands (such as rm) will report an error of "parameter list too long" if there are too many parameters, and thus cannot be executed. Using xargs instead does not have this problem because it can execute the command once for each parameter.

$ find . -name "*.txt" | xargs grep "abc"

After the above command finds all TXT files, it searches each file to see if it contains the string abc.

-n specifies the number of items

Although the -L parameter solves the multi-line problem, sometimes users enter multiple items on the same line.

$ xargs find -name
"*.txt" "*.md"
find: 路径必须在表达式之前: *.md

The above command uses two items on the same line as command line parameters, resulting in an error.

The -n parameter specifies how many items to pass as command line parameters each time.

$ xargs -n 1 find -name

The above command specifies that each item (-n 1) of standard input is used as a command line parameter and the command (find -name) is executed once.

-I option
If xargs wants to pass command line parameters to multiple commands, you can use the -I parameter.

-I Specifies a substitution string for each command line argument.

$ cat jiyik.txt
jiyik_one
jiyik_two
jiyik_three
$ cat jiyik.txt | xargs -I file sh -c 'echo file; mkdir file'
jiyik_one
jiyik_two
jiyik_three

Check that three directories have been generated in the current directory.

$ ls
jiyik_one  jiyik_three  jiyik_two

In the above code, jiyik.txt is a text file containing three lines. We want to execute two commands (echo and mkdir) for each command line parameter. -I file is used to indicate that file is a replacement string for the command line parameter. When the command is executed, the specific parameter will replace the two files in echo file; mkdir file.

xargs command -I option execution results

--max-procs parameter

By default, xargs uses only one process to execute commands. If a command needs to be executed multiple times, it must wait until the previous execution is completed before the next execution.

The --max-procs parameter specifies how many processes are used to execute commands in parallel. --max-procs 2 means that at most two processes are used at the same time, and --max-procs 0 means that there is no limit on the number of processes.

$ docker ps -q | xargs -n 1 --max-procs 0 docker kill

The above command means shutting down as many Docker containers as possible at the same time, which will make the running speed much faster.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Restart PostgreSQL in Ubuntu 18.04

Publish Date:2025/04/09 Views:72 Category:PostgreSQL

This short article shows how to restart PostgreSQL in Ubuntu. Restart PostgreSQL Server in Ubuntu You can restart Postgres server in Ubuntu using the following command. Order: sudo service postgres restart Sometimes the above command does n

Issues to note when installing Apache on Linux

Publish Date:2025/04/08 Views:78 Category:OPERATING SYSTEM

As the most commonly used web server, Apache can be used in most computer operating systems. As a free and open source Unix-like operating system, Linux and Apache are a golden pair. This article will introduce the installation and use of A

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial