A Linux command a day (21): find -xargs command

post thumb
Devops
by Admin/ on 03 Oct 2021

A Linux command a day (21): find -xargs command


When using the -find command with the -exec option to process matched files, the -find command passes all the matched files together to exec for execution. However, some systems have a limit on the length of commands that can be passed to exec, so that after a few minutes of running the find command, an overflow error will occur. The error message is usually “argument column too long” or “argument column overflowed”. This is where the xargs command comes in handy, especially when used in conjunction with the find command.

The find command passes the matched files to the xargs command, and the xargs command fetches some of the files at a time instead of all of them, unlike the -exec option. This allows it to process the first batch of files fetched first, then the next, and so on.

In some systems, using the -exec option launches a process for each matched file, rather than executing all the matched files at once as arguments; this can be inefficient in some cases because of the problem of too many processes and performance degradation, whereas with 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 to be fetched at each time will be determined by the options of the command and the corresponding adjustable parameters in the system kernel.

Usage examples.

Example 1: Find every common file in the system and use the xargs command to test what kind of file they belong to

Command:

find . -type f -print | xargs file

output:

[root@localhost test]# 11

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) in the whole system and save the result to /tmp/core.log file

Commands:

find / -name "core" -print | xargs echo "" >/tmp/core.log

Output.

[root@localhost test]# find / -name "core" -print | xargs echo "" >/tmp/core.log

[root@localhost test]# cd /tmp

[root@localhost tmp]# 11

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 for which the user has read, write, and execute permissions, and retrieve the corresponding write permissions

Command:

find . -perm -7 -print | xargs chmod o-w

output:

[root@localhost test]# 11

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 o-w

[root@localhost test]# 11

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:

After executing the command, the permissions of the folders scf, test3 and test4 are changed

Example 4: Use grep command to search for the word hostname in all common files

Command:

find . -type f -print | xargs grep "hostname"

output:

[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: Search for the word hostnames in all common files in the current directory with the grep command

Command.

find . -name \* -type f -print | xargs grep "hostnames"

output:

[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 the shell.

Example 6: Using xargs to execute the mv

Command:

find . -name "*.log" | xargs -i mv {} test4

** Output:**

[root@localhost test]# 11

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]# 11

Total 0 [root@localhost test4]# cd .

[root@localhost test]# find . -name "*.log" | xargs -i mv {} test4

[root@localhost test]# 11

Total 12

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]# cd test4/

[root@localhost test4]# 11

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]#

Example 7: execution of xargs after find prompts xargs: argument line too long Solution.

Command:

find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f

output:

[root@pd test4]# find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f

rm -f 

[root@pdtest4]#

Description:

-11 is processing one at a time; -t is printing out the command before processing

Example 8:** Use the -i parameter to replace the default preceding output with {}; the -I parameter can specify other replacement characters, such as []** in the example

Command:

output:

[root@localhost test]# 11

Total 12

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]# cd test4

[root@localhost test4]# find . -name "file" | xargs -I [] cp [] ...

[root@localhost test4]# 11

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]# 11

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:

Use the -i parameter to replace the default preceding output with {}, the -I parameter can specify other replacement characters, such as [] in the example

Example 9:** Use of the -p argument to xargs**

Commands:

find . -name "*.log" | xargs -p -i mv {} .

Output:

[root@localhost test3]# 11

Total 0

-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log

[root@localhost test3]# cd .

[root@localhost test]# 11

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]# 11

Total 0

[root@localhost test3]# cd ...

[root@localhost test]# 11

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 argument prompts you to confirm whether to execute the subsequent command, y to execute, n not to execute.


Reference:

www.cnblogs.com/peida/archive/2012/11/15/2770888.html

comments powered by Disqus