In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
-exec and xargs are often used in conjunction with find to manipulate files.
-exec is an option for find, not the exec command. For the find command, the command option parameter is very special, which is not the common single-letter option of UNIX-style, but is composed of words, professionally called predicate, which constitutes a new syntax specification of UNIX-style command.
Find why to add-exec, which has to talk about UNIX-sytle,UNIX with special emphasis on the function of commands and magic, like to use a command to achieve multiple functions. Therefore, the experts are also interested in single-line commands, even though they do not hesitate to use pipes to string multiple commands together. I think the boss who developed the find command must think that only find to something is too easy to reflect his own UNIX-sytle style, so he added the option-exec to make further flexible operations on the things that find comes to.
BTW, learn more about UNIX-style, you will gradually understand and integrate into the UNIX culture, have a bone understanding of all kinds of commands, and it will be easier to use.
Xargs is obviously different from-exec. Xargs is a single command and is more powerful. Xargs literally means that args represents multiple parameters (arguments). With multiple parameters, you can associate them with a list of parameters. The first x indicates execution or eXecute. To sum up, it is to manipulate the parameter list.
To put it simply,-exec is to find a file to execute a command operation, xargs is to find all the files first, and then unify the command operation. For example, for grep operations from find to 1000 files, find will fork a child process for the first file found, and then grep the child process. At this time, the find main process is waiting until the grep operation of the child process out of fork is completed, before looking for the next file. This is a very casual way to deal with it. The xargs command is different. Xargs finds all the files at once and uses a grep command to do it all at once. As for the fact that xargs has to group very long parameter lists, I won't go into details.
Xargs is much more efficient than-exec. To take a practical example of competition, at a glance.
[root@localhost ~] # time find / usr/include-type f-exec grep NFS_VERSION {}\; version NFS_VERSION {# define NFS_VERSION 2real 0m17.328suser 0m5.418ssys 0m8.421s [root@localhost ~] # time find / usr/include-type f | xargs grep NFS_VERSION/usr/include/rpcsvc/nfs_prot.x: version NFS_VERSION {/ usr/include/rpcsvc/nfs_prot.h:#define NFS_VERSION 2real 0m0.095suser 0m0.031ssys 0m0.061s
Look at the sys index, 8.421s vs 0.061s, 140 times the gap.
Xargs is very powerful, but problems will occur when using files that are not properly named, such as "a b.txt", which will be understood as "a" and "b.txt". The solution is to add the "- print0" option to find. (note that the options for the number 0find are all made up of words, so it is print0, a compound word). The meaning of finding a file is to add the upper character ('\ 0') to the file name as the file separator, and then you must add the "- 0" option to the xargs to indicate that the previous file parameter list from find uses'\ 0' as the file separator, so that you can correctly identify and distinguish each file. In the above example, the more standardized way of writing is as follows:
[root@localhost ~] # time find / usr/include-type f-print0 | xargs-0 grep NFS_VERSION/usr/include/rpcsvc/nfs_prot.x: version NFS_VERSION {/ usr/include/rpcsvc/nfs_prot.h:#define NFS_VERSION 2real 0m0.102suser 0m0.036ssys 0m0.065s
This reveals another contradiction, the contradiction between the naming of files and the identification and operation of files. UNIX-sytle but the file naming rules are quite loose oh, it seems that there is no specification, later if you encounter a file name problem and then find a way to solve. We will try to ask, why not first strictly regulate the naming of the file, so as to avoid the trouble of subsequent operation. However, a core concept of UNIX is the file, file naming should of course be open, so as to meet different file naming needs, as for the trouble caused by the file name, never mind, put aside, later who encounter who will solve, this is the UNIX culture.
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.