In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to use xargs commands in Linux", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use xargs commands in Linux" this article.
Linux common commands the xargs command is a filter for passing parameters to other commands and a tool for combining multiple commands. It is good at converting standard input data into command-line parameters, and xargs can process pipes or stdin and convert them into command parameters for specific commands.
A filter that xargs passes parameters to other commands
Add that xargs can also convert single-line or multi-line text input to other formats, such as multi-line to single-line, single-line to multi-line. The default command for xargs is echo, and spaces are the default delimiters. This 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 one of the important components for building one-line commands.
The xargs command uses xargs as a replacement tool to read the input data and reformat the output.
Define a test file with multiple lines of text data:
Cat test.txt a b c d e f g h i j k l m n o p q r st u v w x y z multi-line input single-line output:
Cat test.txt | xargs a b c d e f g h i j k l m n o p q r st u v w x y z uses-n for multi-line output-n option:
Cat test.txt | xargs-n3 a b c d e f g h i j k l m n o p q r st u v w x y z uses the-d split input-d option to customize a delimiter:
Echo "nameXnameXnameXname" | xargs-dX name name name name in combination with the-n option:
Echo "nameXnameXnameXname" | xargs-dX-N2 name name name name read stdin read stdin, pass the formatted parameters to the command
Suppose a command is sk.sh and a file arg.txt that saves parameters:
The contents of the #! / bin/bash # sk.sh command to print out all parameters. Echo $* arg.txt file content:
Cat arg.txt aaa bbb ccc combines-I with an option of xargs, using-I to specify a replacement string {}, which is replaced when xargs is extended. When-I is used with xargs, each parameter command is executed once:
Cat arg.txt | xargs-I {}. / sk.sh-p {}-l-p aaa-l-p bbb-l-p ccc-l copy all image files to the / data/images directory:
Ls * .jpg | xargs-N1-I cp {} / data/images with find command using xargs 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:
Find. -type f-name "* .log"-print0 | xargs-0 rm-log-0 uses\ 0 as the delimiter.
Count the lines of all php files in a source code directory:
Find. -type f-name "* .php"-print0 | xargs-0 wc-l find all jpg files and compress them:
Find. -type f-name "* .jpg"-print | xargs tar-czvf images.tar.gz print out the command executed combined with the-t option to print out the command executed by xargs
Ls | xargs-t-I {} echo {} will output the list of files in the current directory and the echo command executed
Use the-p option to confirm the execution of the command-p option will pop up confirmation when executing each command. You can use the-p parameter when you need to confirm each operation very accurately, for example, to find the .log file in the current directory, and each deletion needs to be confirmed:
Find. -maxdepth 1-name "* .log" | xargs-p-I {} rm {} execute multiple commands using the-I option allows xargs to execute multiple commands
Cat foo.txt one two three cat foo.txt | xargs-I% sh-c 'echo%; mkdir%' one two three ls one two three other applications xargs other applications
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 sub-Shell (Subshells) launches another command interpreter when running a shell script, as if your commands were interpreted at the command line prompt, similar to a series of commands in a batch file. Each shell script runs effectively in a child process of the parent shell (parent shell). The parent shell is the process that gives you command indicators in a control terminal or in a xterm window.
Cmd1 | (cmd2; cmd3; cmd4) | cmd5 if cmd2 is cd /, then the working directory of the child Shell will be changed. This change is limited to the child shell, while cmd5 is completely unaware of the change in the working directory. The sub-shell is a sequence of commands embedded in parentheses (), and the variables defined within the sub-Shell are local variables.
The child shell can be used to set temporary environment variables for a set of commands:
COMMAND1 COMMAND2 COMMAND3 (IFS=: PATH=/bin unset TERMINFO set-C shift 5 COMMAND4 COMMAND5 exit 3 # only exits from the child shell. ) # the parent shell is not affected, and the value of the variable is not changed. COMMAND6 COMMAND7 above is all the contents of this article entitled "how to use xargs commands in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.