In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "the usage of find command in linux system". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The error message is usually "Parameter column too long" or "Parameter column overflow". This is what the xargs command is for, especially when used with the find command.
The find command passes the matching files to the xargs command, while the xargs command fetches only some files at a time, not all of them, unlike the-exec option. In this way, it can first process the first part of the files obtained, then the next batch, and so on.
In some systems, using the-exec option initiates a process to process each matched file, instead of executing all the matched files as parameters at once; in some cases, there will be too many processes and poor system performance, resulting in inefficiency; 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 in batches, and the number of parameters each time will be determined according to the options of the command and the corresponding adjustable parameters in the system kernel.
Examples of use:
Example 1: find every ordinary file in the system, and then use the xargs command to test which type of file they belong to.
Command:
The code is as follows:
Find. -type f-print | xargs file
Output:
The code is as follows:
[root@localhost test] # ll
Total 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
Drwxr-xr-x 6 root root 4096 10-27 01:58 scf
Drwxrwxrwx 2 root root 4096 11-12 19:32 test3
Drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test] # find. -type f-print | xargs file
. / log2014.log: empty
. / log2013.log: empty
. / log2012.log: ASCII text
[root@localhost test] #
Example 2: find the memory information dump file (core dump) throughout the system, and then save the results to the / tmp/core.log file
Command:
The code is as follows:
Find /-name "core"-print | xargs echo "" > / tmp/core.log
Output:
The code is as follows:
[root@localhost test] # find /-name "core"-print | xargs echo "" > / tmp/core.log
[root@localhost test] # cd / tmp
[root@localhost tmp] # ll
Total 16
-rw-r--r-- 1 root root 1524 11-12 22:29 core.log
Drwx- 2 root root 4096 11-12 22:24 ssh-TzcZDx1766
Drwx- 2 root root 4096 11-12 22:28 ssh-ykiRPk1815
Drwx- 2 root root 4096 11-03 07:11 vmware-root
Example 3: find all files in the current directory that users have read, write and execute permissions, and withdraw the corresponding write permissions
Command:
The code is as follows:
Find. -perm-7-print | xargs chmod Omurw
Output:
The code is as follows:
[root@localhost test] # ll
Total 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
Drwxr-xr-x 6 root root 4096 10-27 01:58 scf
Drwxrwxrwx 2 root root 4096 11-12 19:32 test3
Drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test] # find. -perm-7-print | xargs chmod Omurw
[root@localhost test] # ll
Total 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
Drwxr-xr-x 6 root root 4096 10-27 01:58 scf
Drwxrwxr-x 2 root root 4096 11-12 19:32 test3
Drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test] #
Description:
Permissions for folders scf, test3, and test4 all change after the command is executed
Example 4: use the grep command to search for the word hostname in all ordinary files
Command:
The code is as follows:
Find. -type f-print | xargs grep "hostname"
Output:
The code is as follows:
[root@localhost test] # find. -type f-print | xargs grep "hostname"
. / log2013.log:hostnamebaidu=baidu.com
. / log2013.log:hostnamesina=sina.com
. / log2013.log:hostnames=true [root@localhost test] #
Example 5: use the grep command to search for the word hostnames in all ordinary files in the current directory
Command:
The code is as follows:
Find. -name\ *-type f-print | xargs grep "hostnames"
Output:
The code is as follows:
[root@peida test] # find. -name\ *-type f-print | xargs grep "hostnames"
. / log2013.log:hostnamesina=sina.com
. / log2013.log:hostnames=true [root@localhost test] #
Description:
Note that in the above example,\ is used to cancel the special meaning of * in the find command in shell.
Example 6: use xargs to execute mv
Command:
The code is as follows:
Find. -name "* .log" | xargs-I mv {} test4
Output:
The code is as follows:
[root@localhost test] # ll
Total 316
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
Drwxr-xr-x 6 root root 4096 10-27 01:58 scf
Drwxrwxr-x 2 root root 4096 11-12 22:54 test3
Drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test] # cd test4/
[root@localhost test4] # ll
Total 0 [root@localhost test4] # cd..
[root@localhost test] # find. -name "* .log" | xargs-I mv {} test4
[root@localhost test] # ll
Total 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
Drwxrwxr-x 2 root root 4096 11-13 05:50 test3
Drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test] # cd test4/
[root@localhost test4] # ll
Total 304
-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:54 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:54 log2014.log
[root@localhost test4] #
After the instance 7:find, execute xargs prompt xargs: argument line too long solution:
Command:
The code is as follows:
Find. -type f-atime + 0-print0 | xargs-0-L1-t rm-f
Output:
The code is as follows:
[root@pd test4] # find. -type f-atime + 0-print0 | xargs-0-L1-t rm-f
Rm-f
[root@pdtest4] #
Description:
-L1 is to process one at a time;-t is to print out the command before processing
Example 8: the default preceding output of the-I parameter is replaced by {}, and the-I parameter can specify other substitute characters, such as [] in the example.
Command:
Output:
The code is as follows:
[root@localhost test] # ll
Total 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
Drwxrwxr-x 2 root root 4096 11-13 05:50 test3
Drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test] # cd test4
[root@localhost test4] # find. -name "file" | xargs-I [] cp []..
[root@localhost test4] # ll
Total 304
-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:54 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:54 log2014.log
[root@localhost test4] # cd..
[root@localhost test] # ll
Total 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log
Drwxr-xr-x 6 root root 4096 10-27 01:58 scf
Drwxrwxr-x 2 root root 4096 11-13 05:50 test3
Drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test] #
Description:
The default preceding output using the-I parameter is replaced by {}, and the-I parameter can specify other substitute characters, such as [] in the example.
The use of the-p parameter of instance 9:xargs
Command:
The code is as follows:
Find. -name "* .log" | xargs-p-I mv {}..
Output:
The code is as follows:
[root@localhost test3] # ll
Total 0
-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log
[root@localhost test3] # cd..
[root@localhost test] # ll
Total 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log
Drwxr-xr-x 6 root root 4096 10-27 01:58 scf
Drwxrwxr-x 2 root root 4096 11-13 06:06 test3
Drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test] # cd test3
[root@localhost test3] # find. -name "* .log" | xargs-p-I mv {}..
Mv. / log2015.log.. ?... y
[root@localhost test3] # ll
Total 0 [root@localhost test3] # cd..
[root@localhost test] # ll
Total 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log
-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log
Drwxr-xr-x 6 root root 4096 10-27 01:58 scf
Drwxrwxr-x 2 root root 4096 11-13 06:08 test3
Drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test] #
Description:
The-p parameter prompts you to confirm whether to execute the following command, y execution, n do not execute.
This is the end of the introduction to the usage of the find command in the linux system. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.