Get the App
SLTechnology News&Howtos  ›  Servers  › 

Use linux's xargs command filter

Shulou Source: shulou.com Published: 2022-06-02 14:44:48 09月22日 Update

Xiaobian to share with you the use of linux xargs command filter, I believe most people still do not know how to share this article for everyone to learn, I hope you read this article after the harvest, let us learn the method together!

The xargs command is a filter for passing arguments to other commands and a tool for combining multiple commands. It is good at converting standard input data into command line arguments, xargs can handle pipes or stdin and convert them into command arguments for specific commands.

Xargs can also convert single or multiline text input to other formats, such as multiline to single line or single-line to multiline. The default command for xargs is echo, and spaces are the default delimiter. This means that input piped to xargs will contain newlines and whitespace, but with xargs processing newlines and whitespace will be replaced by whitespace. Xargs is one of the important components of building a one-line command.

Syntax format: xargs [parameter]

Common parameters:

-n multiline output-d customize a delimiter-I specify a replacement string {}-t print out the commands xargs executes-p pop up confirmation when executing each command

Reference Example

Multiline output:

[root@linuxcool ~]#cat test.txt | xargs -n3

Customize a delimiter:

[root@linuxcool ~]#echo "nameXnameXnameXname" | xargs -dXname name name name

Specify a replacement string {}:

[root@linuxcool ~]#cat arg.txt | xargs -I {} ./ sk.sh -p {} -l-p aaa -l-p bbb -l-p ccc -l

Print out the commands xargs executes:

[root@linuxcool ~]#ls | xargs -t -I{} echo {}

A confirmation pops up for each command, when you need to confirm each action very accurately:

[root@linuxcool ~]#find . -maxdepth 1 -name "*.log" |xargs -p -I{} rm {} This is all about using linux xargs command filters. Thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Tags: Commands parameters multilines filters delimiters learning input content characters strings formats spaces whitespace pipes processing output important not much multiple most Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info vpn Redmi MySQL