The Linux find command combined with xargs method

  

The find command is mainly used for file search. It has been described in detail in the previous article (see the system home Linux find command common usage summary), today Xiaobian wants to give Everyone introduced the use of the Linux find command and the xargs command. Let's take a look at it. When

In the process -exec option to use the find command to match the file, find the command will be passed along to all matching files to execute exec. However, some systems have restrictions on the length of commands that can be passed to exec, so that after a few minutes of the find command, an overflow error will occur.

The error message is usually "The parameter column is too long" or "Parameters" Column overflow & rdquo;. This is where the xargs command is used, especially with the find command.

The find command passes the matching file to the xargs command, while the xargs command only gets a portion of the file at a time instead of all, unlike the -exec option. This way it can process the first part of the file first, then the next one, and continue with it.

In some systems, using the -exec option will initiate a corresponding process for processing each matched file, not all of the matched files will be executed as parameters once; in some cases There are too many processes and system performance degradation, so it is not efficient; while using the xargs command there is only one process. In addition, when using the xargs command, whether to get all the parameters at a time, or to get the parameters in batches, and the number of parameters obtained each time will be determined according to the options of the command and the corresponding adjustable parameters in the system kernel.

Use Case:

Instance 1: Find each regular file in the system, and then use the xargs command to test which 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]#

Instance 2: Find the memory dump file (core dump) in the entire system, then save the result to /The

command in the tmp/core.log file:

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&rdquo ; -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
Previous1234Next page Total 4 pages

Copyright © Windows knowledge All Rights Reserved