Home Operation and Maintenance Linux Operation and Maintenance Detailed explanation of various uses of xargs command techniques in Linux

Detailed explanation of various uses of xargs command techniques in Linux

May 28, 2017 am 11:39 AM

xargs is a filter for passing parameters to commands, and is also a tool for combining multiple commands. The following article mainly introduces to you the relevant information about the usage of xargs command in linux. Friends who need it can refer to it. Let’s take a look with the editor.

Preface

The xargs command reformats the received data and then provides it as a parameter to other commands. The xargs command is introduced below. Let’s take a look at various techniques for using commands.

1. Convert multi-line input into single-line input:

[root@host1 test]# echo -e "1 2 3 4 5 \n6 7 8 \n9 10 11 12" >example.txt
[root@host1 test]# cat example.txt 
1 2 3 4 5 
6 7 8 
9 10 11 12
[root@host1 test]# cat example.txt |xargs 
1 2 3 4 5 6 7 8 9 10 11 12
Copy after login

Convert single-line input into multi-line output:

[root@host1 test]# cat example.txt | xargs -n 3
1 2 3
4 5 6
7 8 9
10 11 12
Copy after login

Customize the delimiter for conversion (the default delimiter is a space):

[root@host1 test]# echo "Hello:Hello:Hello:Hello" | xargs -d : -n 2
Hello Hello
Hello Hello
Copy after login

2. Use it in the script:

[root@host1 test]# cat echo.sh 
#!/bin/bash
echo $* '^-^'
Copy after login

When the parameters are passed to echo.sh, it will print out these parameters and end with "^-^":

[root@host1 test]# echo -e "Tom\nHarry\nJerry\nLucy" > args.txt
[root@host1 test]# cat args.txt | xargs bash echo.sh 
Tom Harry Jerry Lucy ^-^
[root@host1 test]# cat args.txt | xargs -n 2 bash echo.sh 
Tom Harry ^-^
Jerry Lucy ^-^
Copy after login

In the above example, we The parameter sources are put into the args.txt file, but in addition to these parameters, we also need some fixed parameters, such as:

[root@host1 test]# bash echo.sh Welcome Tom 
Welcome Tom ^-^
Copy after login

During the execution of the above command, Tom is the variable , the rest are constants, we can extract the parameters from "args.txt" and provide them to the command in the following way:

[root@host1 test]# bash echo.sh Welcome Tom 
[root@host1 test]# bash echo.sh Welcome Herry
[root@host1 test]# bash echo.sh Welcome Jerry
[root@host1 test]# bash echo.sh Welcome Lucy
Copy after login

At this time we need to use xargs -I command:

[root@host1 test]# cat args.txt | xargs -I {} bash echo.sh Welcome {} 
Welcome Tom ^-^
Welcome Harry ^-^
Welcome Jerry ^-^
Welcome Lucy ^-^
Copy after login

-I {} specifies the replacement of string. For each command parameter, the string {} will be replaced by the parameters read from stdin. ,

When -I is used, the command is executed in a loop. If there are 4 parameters, the command will be executed 4 times together with {}, in each execution {} will be replaced with the corresponding parameters.

3. Use with find

xargs and find are a very good combination, but we usually use it in the wrong way Ways to use them, such as:

[root@host1 test]# find . -type f -name "*.txt" -print | xargs rm -f
Copy after login

This is dangerous, sometimes delete unnecessary files, if the file name contains a space character (' '), then xargs It is possible to think that they are delimiters (e.g. file text.txt will be mistaken for file and text.txt by xargs).

If we want to use the output of find as the input of xargs, we must use -print0 in conjunction with find to separate the output with the characters null ('\0'), and use find to find out All .txt files, and then use xargs to delete these files:

[root@host1 test]# find . -type f -name "*.txt" -print0 | xargs -0 rm -f
Copy after login

This way you can delete all .txt files, xargs -0 Use \0 as the input delimiter.

4. Use the while statement and subshell

[root@host1 test]# cat files.txt | (while read arg ;do cat $arg;done)
Copy after login

This command is equivalent to:

[root@host1 test]# cat files.txt | xargs -I {} cat {}
Copy after login

In while loop, cat $arg can be replaced with any number of commands, so that we can execute multiple commands on the same parameter, or we can use The output is passed to other commands. This technique is suitable for a variety of problem scenarios. Multiple commands within subshellOperator can be run as a whole.

Summarize

The above is the detailed content of Detailed explanation of various uses of xargs command techniques in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Clair Obscur: Expedition 33 - How To Get Perfect Chroma Catalysts
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

How to set important Git configuration global properties How to set important Git configuration global properties Apr 17, 2025 pm 12:21 PM

There are many ways to customize a development environment, but the global Git configuration file is one that is most likely to be used for custom settings such as usernames, emails, preferred text editors, and remote branches. Here are the key things you need to know about global Git configuration files.

See all articles