One linux command per day: xargs of find command

Keywords: ssh ascii Vmware shell

When processing matched files with the - exec option of the find command, the find command passes all matched files together to exec for execution. But some systems limit the length of commands that can be passed to exec, so that overflow errors occur after the find command runs for a few minutes. Error messages are usually "parameter column is too long" or "parameter column overflow". This is where the xargs command is used, especially with the find command.

The find command passes the matched file to the xargs command, while the xargs command takes only part of the file at a time, not all, unlike the - exec option. In this way, it can process the first part of the files, then the next batch, and so on.

In some systems, using the - exec option will initiate a corresponding process to process every matched file, not all matched files will be executed as parameters at once; in some cases, there will be too many processes, system performance degradation, and therefore inefficiency; while using the xargs command, there will be 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.

Use examples:

Example 1: Find every common file in the system, and then use the xargs command to test which files they belong to
Order:

find . -type f -print | xargs file

Output:

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

 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]# 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 with read, write, and execute privileges under the current directory and withdraw the corresponding write privileges
Order:

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

Output:

[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 o-w
[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]#

Explain:

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

Example 4: Search for the word hostname in all common files with the grep command
Order:

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
Order:

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

Explain:

Note that in the example above, is used to cancel the special meaning of * in the shell in the find command.

Example 6: Executing mv using xargs
Order:

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

Output:

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

Example 7: find and execute xargs prompt xargs: argument line too long solution:
Order:

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

Explain:

- l1 is to process one at a time; - t is to print out commands before processing

Example 8: The default front output of the - I parameter is replaced by {}, and the - I parameter can specify other substitution characters, such as [] in the example.
Order:

find . -name "file" | xargs -I [] cp [] ..

Output:

[root@localhost test]# ll
//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]# 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]#

Explain:

The default front output of the - I parameter is replaced by {}, and the - I parameter can specify other substitution characters, such as [] in the example.

Example 9: Use of - p parameter of xargs
Order:

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

Output:

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

Explain:

- The p parameter prompts you to confirm whether to execute the following commands, y executes, n does not execute.

Posted by d~l on Tue, 26 Mar 2019 06:36:29 -0700