exec option to find commands and command xargs

Own "Linux System Administration Guide for Beginners - based on CentOS 7.6" was published in October 2019, for the overall quality of this book feel is quite satisfactory, but was limited to the level of knowledge, still not described in some places clearly, so in the course of the lecture are constantly revised. For example, recently spoke of the find exec command options, many students questioned, so I had to rewrite this part of the original book, hoping the revised second edition.
exec option to find commands and command xargs

"-Exec" option to find the results as a file for processing

"-Exec" option on the command further processing to be performed later to keep up, you can use the command "{}" indicates the find command to results, and finally you must add "\;" indicates the end of the command (Note that there are spaces in front ).
For example, to find out the / tmp directory of all the suffix ".txt" file and delete it.
[root@localhost ~]# find /tmp -name "*.txt" -exec rm -f {} \;
Many students here will be wondering why this pipe symbol "|" (covered in Section 2.9.5) can not achieve the above does it? For example, we do the following test:

[root@localhost ~]# touch /tmp/{a,b,c}.txt  #在/tmp目录中生成3个测试文件
[root@localhost ~]# find /tmp -name "*.txt" | rm -f     #利用管道结合rm删除
[root@localhost ~]# find /tmp -name "*.txt"     #测试文件仍然存在
/tmp/a.txt
/tmp/b.txt
/tmp/c.txt
[root@localhost ~]# find /tmp -name "*.txt" -exec rm -f {} \;   #利用exec结合rm删除
[root@localhost ~]# find /tmp -name "*.txt" #测试文件被成功删除

the results of the most important role exec option is that you can find commands found in the file as to deal with, but by default, the result of the find command is found to be treated as text information processing.
How to understand the above paragraph it? For example, for the implementation of "find / tmp -name" * .txt "" command found three documents: /tmp/a.txt,/tmp/b.txt,/tmp/c.txt, default case just find command the three files that meet the search criteria to find and put them in the name of the output on the screen, so we can see on the screen is only three lines of text information. For text messages, the contents of the file can be used before the introduction of the operation command to be processed, such as with wc command counts lines, filtered with grep commands.

[root@localhost ~]# find /tmp -name "*.txt" | wc -l     #统计find找到的文件数量
3
[root@localhost ~]# find /tmp -name "*.txt" | grep 'a'  #对find的结果进行过滤
/tmp/a.txt

For text messages, file and directory operations command previously introduced can not be dealt with, such as cp, mv, rm, etc., because these objects must be operated by a command file. At this point exec option can come in handy, because its main role is to find the results of the find command is no longer seen as a text message, but as a file. So if you need to find the results of further processing with file manipulation commands, then you need to combine exec option.
For example, the Find / file "init" at the beginning of the boot directory, and copy it to the / tmp directory.
[root@localhost ~]# find /boot -name "init*" -exec cp {} /tmp \;
For example, to find the size of 1MB or more files in the / etc directory, and its details humanized displayed.

[root@localhost ~]# find /etc -size +1M -exec ls -lh {} \;
……
-r--r--r--. 1 root root 7.6M 9月   5 18:53 /etc/udev/hwdb.bin
-rw-r--r--. 1 root root 1.4M 4月  11 09:32 /etc/brltty/zh-tw.ctb

xargs command

When the found results for further processing using the -exec option in the find command, sometimes problems may arise. This is because the -exec is found as a result of the find at once to the back of the command to be processed, sometimes find may find a large number of documents, beyond the parameters behind the command can handle, then there will overflow error, the error message is usually "argument list too long" or "parameter column overflow", then you can use the xargs command. xargs While Linux itself is an independent command, but usually are used with the find command. By xargs, you can find the results found will be divided after the command sent in batches for processing, in order to avoid overflow problems.
xargs command through the pipeline needs to be used in conjunction with the find command, xargs command format "find ...... | xargs commands".
Let us first prepare a test file.

[root@localhost ~]# mkdir /tmp/pass
[root@localhost ~]# echo "password:123" >> /tmp/pass/test.txt

Assume stored in the / tmp directory, a large number of files in one of these files is stored in a password, the keyword is "password", we now hope to be able to store this password file to find out.
If you use -exec option to find commands, you can execute the following command:

[root@localhost ~]# find /tmp -type f -exec grep "password" {} \;
password:123

It can be found, though to find out your password by the above command, but does not show the stored password file name. The following change to do with the xargs command to achieve this requirement, xargs can file containing the keywords displayed together.

[root@localhost ~]# find /tmp -type f | xargs grep "password"
/tmp/pass/test.txt:password:123

As another example, we want the / tmp directory and / tmp all subordinate subdirectories, file name ".txt" as a suffix files are copied to the / root directory. : If the option to achieve a find command -exec
[root@localhost ~]# find /tmp -name "*.txt" -exec cp {} /root \;
"{}" means to represent the results of the find command to the xargs command if implemented, the same need, and the need to add -i option xargs command.
[root@localhost ~]# find /tmp -name "*.txt" | xargs -i cp {} /root
Can be found through these few examples, xargs command and find -exec command options are basically the same, so if -exec option to meet the requirements, then you do not need to use the xargs command. The main purpose xargs command is that it can find the results to find the batch command to avoid overflow errors.
For example, in the / etc directory, a total of 2507 regular file.

[root@localhost ~]# find /etc -type f | wc -l
2507

If we want to find out the / etc directory of all the files that contain the keyword "PermitRootLogin", namely to achieve these two methods:

[root@localhost ~]# find /etc -type f -exec grep "PermitRootLogin" {} \;
#PermitRootLogin yes
# the setting of "PermitRootLogin without-password".
[root@localhost ~]# find /etc -type f | xargs grep "PermitRootLogin" 
/etc/ssh/sshd_config:#PermitRootLogin yes
/etc/ssh/sshd_config:# the setting of "PermitRootLogin without-password".

Can be found in the realization of the method -exec option, there is a clear Caton, if the amount of data then a large number, it might cause an overflow. The use xargs command to achieve, on the one hand more quickly, on the other hand will not overflow problem, but also what is displayed in more detail. So during this type of operation, more recommended to use xargs command.

Guess you like

Origin blog.51cto.com/yttitan/2455746