In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
My own Guide for beginners in Linux system Management-based on CentOS 7.6 was published in October 2019. I was generally satisfied with the quality of the book, but limited to the level of knowledge at that time, the description was still not very clear, so it was constantly revised during the lecture. For example, recently, when talking about the processing actions of the find command, many students raised questions, so I rewrote this part of the original book, hoping to correct it when it was republished.
Processing action
The find command can not only find, but also further process the results found, which requires a "processing action".
For example, find all the symbolic link files in the / usr/bin directory.
[root@localhost ~] # find / usr/bin-type l
The result of the above command shows only the file name, and if we also want to see which source file each symbolic link file points to, we can add the processing action "- ls" at the end of the find command.
[root@localhost ~] # find / usr/bin-type l-ls50346791 0 lrwxrwxrwx 1 root root 3 February 14 2019 / usr/bin/captoinfo-> tic
It is important to note that this processing action "- ls" is part of the find command, not the ls command described earlier, so you cannot add various options to the ls command after the processing action "- ls". For example, if we want to find all the files in the / etc directory that are larger than 1MB and humanize their details, executing the following command will cause an error.
[root@localhost ~] # find / etc-size + 1m-ls-lhfind: unknown assertion "- lh"
To achieve this requirement, you need to rely on another processing action of the find command, "- exec". This processing action can treat the results found by the find command as a file, followed by "- exec" to keep up with the further processing of the command to be executed, and the symbol "{}" needs to be used in the command to indicate the results found by the find command. ";" must be added at the end of the command to indicate the end of the command (note that there is a space in front of it).
For example, to find all the files in the / etc directory that are larger than 1MB and humanize their details, the following command is the right thing to do. In this command, ls is the ls command introduced earlier. With the help of-exec, we pass the results found by the find command to the ls command in the form of a file to continue processing.
[root@localhost ~] # find / etc-size + 1m-exec ls-lh {}\;-rw-. 1 root root 3.8m November 3 2018 / etc/selinux/targeted/active/policy.kern
For example, find all the files in the / tmp directory with the suffix ".txt" and delete them.
[root@localhost ~] # find / tmp-name "* .txt"-exec rm-f {}\
Many students here will wonder why the above operation can not be achieved with the pipe character "|" (described in Section 2.9.5). For example, we do the following test:
[root@localhost ~] # touch / tmp/ {arecom b C} .txt # generate three test files in the / tmp directory [root@localhost ~] # find / tmp-name "* .txt" | rm-f # delete [root@localhost ~] # find / tmp-name "* .txt" # test files using pipes combined with rm still exist / tmp/a.txt/tmp/b.txt/tmp/c.txt [root@localhost ~] # find / tmp-name "* .txt"-exec rm {}\ # deleting [root@localhost] # find / tmp-name "* .txt" # using exec and rm successfully deleted the test file
Processing actions-the main function of exec is to treat the results found by the find command as files, while by default, the results found by the find command are treated as text messages.
How to understand the above paragraph? For example, for the three files found by executing the "find / tmp-name" * .txt "" command: / tmp/a.txt, / tmp/b.txt, / tmp/c.txt, by default the find command just finds the three files that meet the search criteria and outputs their names on the screen, so what we see on the screen is only three lines of text information. For text information, you can use the file content manipulation commands described earlier, such as counting lines with the wc command, filtering with the grep command, and so on.
[root@localhost ~] # find / tmp-name "* .txt" | wc-l # count the number of files found by find 3 [root@localhost ~] # find / tmp-name "* .txt" | grep 'a' # filter the results of find / tmp/a.txt
For text messages, the file and directory manipulation commands described earlier cannot be processed, such as cp, mv, rm, and so on, because the objects they operate on must be files. At this point-exec can come in handy, because its main function is to treat the results found by the find command not as text messages, but as files. So if you need to further process the results of find with file manipulation commands, you need to combine-exec.
For example, look for files under the / boot directory that start with "init" and copy them to the / tmp directory.
[root@localhost ~] # find / boot-name "init*"-exec cp {} / tmp\
In addition to-ls and-exec, a more common processing action is-delete, which deletes the results found by find directly.
For example, the previous action: find all files in the / tmp directory with the suffix ".txt" and delete them. In fact, the easier way to do this is the following command:
[root@localhost ~] # find / tmp-name "* .txt"-delete
Xargs command
Problems may sometimes occur when you use-exec to further process the found results in the find command. This is because-exec is to send the results found by find to the following command for processing at one time, sometimes find may find a large number of files, which is beyond the scope of the parameters that the later command can handle, and then there will be an overflow error. The error message is usually "parameter column is too long" or "parameter column overflow", then you can use the xargs command. Although xargs itself is a separate Linux command, it is usually used in conjunction with the find command. With xargs, the results found by find can be sent in batches to subsequent commands for processing, thus avoiding overflow problems.
The xargs command needs to be used in conjunction with the find command through a pipeline. The xargs command format is "find … | xargs commands".
Let's prepare a test file first.
[root@localhost ~] # mkdir / tmp/pass [root@localhost ~] # echo "password:123" > > / tmp/pass/test.txt
Suppose there are a large number of files in the / tmp directory, and a password is stored in one of the files with the keyword "password". We now want to be able to find the file in which the password is stored.
If you use the-exec option of the find command, you can execute the following command:
[root@localhost ~] # find / tmp-type f-exec grep "password" {}\; password:123
You can see that although the password was found through the above command, the file name in which the password is stored is not displayed. Instead of using the xargs command to implement this requirement, xargs can display the file where the keyword is located.
[root@localhost ~] # find / tmp-type f | xargs grep "password" / tmp/pass/test.txt:password:123
For example, we want to copy files with the suffix ".txt" in the / tmp directory and all the subordinate subdirectories of / tmp to the / root directory. If you use the-exec of the find command to implement:
[root@localhost ~] # find / tmp-name "* .txt"-exec cp {} / root\
If you do this with the xargs command, you also need to use "{}" to refer to the results found by the find command, and you need to add the-I option to the xargs command.
[root@localhost ~] # find / tmp-name "* .txt" | xargs-I cp {} / root
From these examples, it can be found that the xargs command has basically the same function as the find command-exec to handle actions, so if-exec meets the requirements, then there is no need to use the xargs command. The main purpose of the xargs command is that it can batch the results found by the find command to avoid overflow errors.
For example, there are 2507 ordinary files in the / etc directory.
[root@localhost ~] # find / etc-type f | wc-l2507
If we want to find all the files in the / etc directory that contain the keyword "PermitRootLogin", use 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".
You can find that there are obvious stutters when implemented with the-exec method, which may lead to an overflow if the amount of data is larger. Using the xargs command, on the one hand, is faster, on the other hand, there is no overflow problem, and the content is displayed in more detail. Therefore, it is more recommended to use the xargs command when doing this kind of operation.
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.