Linux xargs Command Usage detailed

Source: Internet
Author: User
Tags stdin

(1) The Find command is based on the attributes of the file, such as file name, file size, owner, owning group, whether it is empty, access time, modification time, etc.

(2) grep is a search based on the contents of the file, and each line of the file is matched in a given pattern (patter).

(3) The Find command passes the matched file to the Xargs command, and the Xargs command takes only a subset of the files at a time instead of all, unlike the-exec option. This allows it to first process a portion of the file that was first fetched, then the next batch, and so on. In some systems, the use of the-EXEC option initiates a corresponding process for processing each matching file, not all of the matching files are executed once as parameters, so that in some cases, there are too many processes and degraded system performance, so the efficiency is not high , while using the Xargs command, there is only one process. In addition, when using the Xargs command, whether to get all the parameters at once or to get the parameters in batches, and the number of parameters to get each time will be determined according to the command's options and the corresponding tunable parameters in the system kernel.

2.find usage:
(1) find/tmp-size +10000c-and-mtime +2 #在/tmp directory to find files larger than 10000 bytes and modified in the last 2 minutes
(2) Find/-user fred-or-user George #在/directory lookup user is Fred or George's file file
(3) Find/tmp! -user Panda #在/tmp directory to find all files that are not part of panda users

3.grep usage:
(1) grep ' test ' d* #显示所有以d开头的文件中包含 Test line
(2) grep ' Test ' AA bb cc #显示在aa, the line containing test in the BB,CC file
(3) grep ' [a-z]\{5\} ' AA #显示所有包含每行字符串至少有5个连续小写字符的字符串的行
(4) grep magic/usr/src #显示 files (without subdirectories) in the/USR/SRC directory contain magic lines
(5) Grep-r magic/usr/src #显示 files (including subdirectories) in the/USR/SRC directory containing magic lines
(6) Grep-w pattern files: matches only the entire word, not part of the string (such as matching ' magic ', not ' magical '),
(7) grep ' Hello ' $ (find.-name *.c)-I.
(8) grep ' Hello './yuhaojin/*.c-i-N

4.find & grep How to use
(1). From the root directory, look for all text files with a. log extension and find the line that contains "ERROR"
Find/-type f-name "*.log" | Xargs grep "ERROR"
(2). Example: Find all text files with an. In extension from the current directory and find the line containing "Thermcontact"
Find. -name "*.in" | Xargs grep "Thermcontact"

5.find & Xargs How to use
(1). Find. -type F-print | Xargs file
Find each normal file in your system, and then use the Xargs command to test what type of file they belong to
(2). Find/-name "core"-print | Xargs echo "" >/tmp/core.log
Find the Memory information dump file (core dump) throughout the system and save the results to the/tmp/core.log file:
(3). Find. -type F-print | Xargs grep "hostname"
Use the grep command to search all common files for the word hostname.
(4). Find./-mtime +3-print|xargs Rm-f–r
Delete everything from 3 days ago (find. -ctime +3-exec RM-RF {};)
(5) Find./-size 0 | Xargs Rm-f &
Delete files with a file size of zero
(6) The Find command with exec and Xargs allows the user to execute almost all commands against the matching file.

The redirection method is as follows:

> filename, redirect standard output to file, overwrite file.
>> filename, redirect standard output to file, append mode.
2> filename, redirecting standard error to file.
&> filename, redirect standard input and standard error to file. Equivalent to using both > and 2>. (Thank Yszzf for your friendship)
< filename, redirect standard input from file

For example, when using Find, some directories do not allow access, and find outputs many rows of errors, real search results are not visible. At this point I will redirect the standard error to the empty device.

1. Find/-name "filename" 2>/dev/null

For example, when using GDB for automated testing, write the command into a file and then send it to GDB.

1. GDB Program < TEST_SCRIPT.GDB

Pipeline
The principle of the pipe is basically introduced clearly. Pipelines allow users to connect standard inputs and standard outputs between different programs, rather than redirecting them to files. The usage is to use the "|" Between commands To connect them.

App1 | App2


Some of the things I used the most:
1. Program output too much, use less slowly look.

1. Verbose_app | Less

2. Search in the program output. A method that can be sent to less to search for in. Here's another command, grep.

Verbose_app | grep pattern
# pattern is the content to be searched. If there are spaces, enclose them in double quotation marks. If you want to use an extended regular expression, use GREP-E.

3. The redirected pipeline will no longer print to the screen since it was redirected. If you want to print to a file again and want to print to the screen, you can use the tee command

Verbose_app | Tee FileName | Another_app



Xargs
If the subsequent program must have command-line arguments and not accept pipe-pass arguments, use Xargs.
1. Show the principle first.

echo "Arg1 arg2" | Xargs app
# equivalent
App Arg1 Arg2

2. Find the file with Find or LS and then tar all the files.

Find. -type F | Xargs Tar-cvzf output.tar.gz

3. Calculate the number of source file lines. A command called WC is used here to calculate the number of words, characters and lines of a file. If the pipe is to the WC, the WC calculates the data as text. File names are only passed from the command line

Find.?NAME"?.C"?O?NAME"?.H"?O?NAME "?cp p " ? o? Nam e " ? hp p " ? name "?. C "?o? name"?. H "?o? name"?. CPP "?o? name"?. HPP "-type F | Xargs wc-l
# or
Find. -type F | Grep-e "\. [CH] (PP)? $ "| Xargs wc-l

The principle of redirection >,2>,< is to replace the standard input with new pipes.
That's what I wrote at the beginning.

Command >/dev/null 2>&1

The problem with this writing is that,> has replaced the standard output with a null device, and then 2>&1 the standard error to "standard output", and "standard output" has now become a null device, which means that the standard error was received on a null device.
The right way is

Command 2>&1 >/dev/null

In addition, Exchange standard output and standard error:

Command 3>&1 1>&2 2>&3

Xargs can solve this problem, Xargs can read the stdin data and distinguish it by a space or a break, separating the stdin data into the parameters of a command. Xargs can convert single-line or multiline text input into other formats.

Xargs can pass data separated by a space or line break in stdin, forming a space-delimited argument (arguments) to be passed to other commands. Because a space is used as a delimiter, xargs may be misjudged when there are spaces in the names of filenames or other meanings. Simply put, Xargs is a filter that passes parameters to other commands and is one of the important components of building a single-line command.

Xargs is used because many commands do not support the use of pipe | To pass parameters, for example:

?
12 find /sbin -perm +700 |ls -l         //这个命令是错误,因为标准输入不能作为ls的参数find /sbin -perm +700 |xargs ls -l   //这样才是正确的

Parameters:-0: If the input stdin contains special characters, such as ', \, spacebar and other characters, this parameter can be restored to a normal character. This parameter can be used in special states.

-E: This is the meaning of EOF (end of file). You can then follow a string, and when Xargs parses the string, it stops working.

-P: The user is queried when the parameters for each command are executed.

-N: The number of subsequent occurrences, with several parameters to be used each time the command is executed.

-D: Use the delimiter of your own definition to separate the parameters.

-I: Uppercase I, the name of each item of the Xargs, is usually assigned to {} on a line, and can be replaced by {}. When I is used, the command executes in a circular fashion. If there are 3 parameters,

Then the command will be executed 3 times together with {}. {} will be replaced with the corresponding parameter in each execution.

-0:如果输入的stdin含有特殊字符,例如反引号`、反斜杠\、空格等字符时,xargs可以将它还原成一般字符。为xargs的默认选项。 -e <flag>,-E <flag>, --eof=<eof-str>:eof是end of file string的意思。flag可以是一个字符串或者是由空格分隔的多个字符串,当xargs分析到这个flag时,就会停止工作。见示例2。 -p:当每次执行一个argument的时候询问一次用户。 -n <num>:表示命令在执行的时候一次使用的argument的个数,由num指定,默认是用所有的参数。 -t:表示先打印命令,然后再执行。 -a <file>:从文件中读入作为sdtin。 -i,-I:其中-I某些Linux版本不支持。将xargs的输出每一项参数,单独赋值给后面的命令,参数需要用{}代替。见示例3。 -r:或者 --no-run-if-empty,当xargs的输入为空的时候则停止xargs,不用再去执行后面的命令了,-r是xargs的默认选项。 -s <num>:命令行的最大字符数,指的是xargs后面那个命令的最大命令行字符数,包括命令、空格和换行符。每个参数单独传入xargs后面的命令。见示例4。 -L <line_num>:设置标准输入中最大的行数作为命令每一次执行的参数。见示例5。 -d <delim>, --delimiter=<delim>: xargs处理标准输入默认是按换行符和空格作为分隔符,输出arguments的分隔符是空格,这里修改xargs处理标准输入时的分隔符。 -x:eXit的意思,主要是配合-s使用,当命令行字符数大于-s指定的数值时,退出xargs。 -P:修改最大的进程数,默认是1,为0时候为 as many as it can。该选项比较少用,目前还不清楚该用法。</delim></delim></line_num></num></file></num></eof-str></flag></flag>

Example:

Example 1: Using the-0 option to delete files with spaces in the Test folder file "file 1.log" "" File 2.log "

[Plain]View PlainCopy
  1. [[email protected] test]# touch "file 1.log" "File 2.log"
  2. [Email protected] test]# ls-l *.log
  3. -rw-r--r--. 1 root root 0 June 18:57 file 1.log
  4. -rw-r--r--. 1 root root 0 June 18:57 file 2.log
  5. [Email protected] test]# find-name ' *.log ' | Xargs rm #直接删除不成功, xargs default to white space characters (spaces, tabs, line breaks)
  6. To split the records.
  7. Rm:cannot remove './file ': No such file or directory
  8. Rm:cannot remove ' 2.log ': No such file or directory
  9. Rm:cannot remove './file ': No such file or directory
  10. Rm:cannot remove ' 1.log ': No such file or directory
  11. [Email protected] test]# find-name ' *.log '-print0 | xargs-0 RM # Find prints out a file name and then prints a null character,
  12. Then tell Xargs to use the null character as a separator for the record.
  13. [Email protected] test]# find-name ' *.log '

Example 2: Use the-e option to print only the string before the character E in "a BC D e RF F"

[Plain]View PlainCopy
    1. [[email protected] test]# echo a BC D e rf F | Xargs-ee
    2. a BC D

Example 3: Using the-P option

[Plain]View PlainCopy
    1. [[email protected] test]# ls A * | Xargs-p RM # before executing RM, please ask
    2. RM A1 A2 A3?... y
    3. [[email protected] test]# LS A *
    4. Ls:cannot Access A *: No such file or directory

Example 4: Batch creation of User1,user2,user3 three users using the-N option

[Plain]View PlainCopy
    1. [Email protected] test]# echo user1 user2 User3 | Xargs-n 1 Useradd
    2. [Email protected] test]# CAT/ETC/PASSWD | Tail-3
    3. User1:x:708:726::/home/user1:/bin/bach
    4. User2:x:709:727::/home/user2:/bin/bach
    5. User3:x:710:728::/home/user3:/bin/bach

(3) Add the suffix name to all files in the current directory.

?
1 ls | xargs -t -i mv {} {}.bak

(4) Sets the maximum number of characters for the command line. The parameters are executed by default in one single incoming command.

?
12345 [[email protected] test]$ echo "01234 56789"|xargs -t -s 11echo 01234 01234echo 56789 56789

(5) Set the number of rows in the standard input each time as a parameter of the command, by default, all rows in the standard input are merged into a single line to the command execution.

?
12345 [[email protected] test]$ echo -e "01234\n56789\n01234"| xargs -t -L 2 echo  echo 01234 56789 01234 56789echo 01234 01234

Example 6: Converting single-line input to multiline output

[Plain]View PlainCopy
    1. [email protected] test]# Cat Example.txt | Xargs-n 3
    2. 1 2 3
    3. 4 5 6
    4. 7 8 9
    5. 10 11 12

Example 7: Separating strings into single or multiple rows using the-D option

[Plain]View PlainCopy
    1. [Email protected] test]# echo "Splitxsplitxsplitxsplit" | Xargs-d X
    2. Split Split split split
    3. [Email protected] test]# echo "Splitxsplitxsplitxsplit" | xargs-d X-n 2 # Combined with the-n option
    4. Split split
    5. Split split

Example 8: Using-I (uppercase I)

[Plain]View PlainCopy
    1. [Email protected] xargsi]# echo this is file1.txt > file1.txt
    2. [Email protected] xargsi]# echo this is File2.txt > file2.txt
    3. [Email protected] xargsi]# echo this is File3.txt > file3.txt
    4. [Email protected] xargsi]# vim Files.txt
    5. [email protected] xargsi]# cat Files.txt
    6. File1.txt
    7. File2.txt
    8. File3.txt
    9. [email protected] xargsi]# Cat Files.txt | Xargs-i {} cat {} is equivalent to Cat Files.txt | (while read Arg; does cat $arg; done)
    10. This is File1.txt
    11. This is File2.txt

Linux xargs Command Usage detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.