One Linux command per day: Xargs of the Find command

Source: Internet
Author: User

When a matching file is processed using the-EXEC option of the Find command, the Find command passes all matching files to exec execution. However, some systems have a limit on the length of the command that can be passed to exec so that an overflow error occurs after the Find command runs for a few minutes. The error message is usually "parameter column 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, and the Xargs command takes only a subset of the files at a time instead of all, unlike the-exec option. This allows it to first process a portion of the file that was first fetched, then the next batch, and so on.

In some systems, the use of the-EXEC option initiates a corresponding process for processing each matching file, not all of the matching files are executed once as parameters, so that in some cases there will be too many processes and degraded system performance, so the efficiency is not high; 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 to get the parameters in batches, and the number of parameters to get each time will be determined according to the command's options and the corresponding tunable parameters in the system kernel.

Usage examples:

Example 1: Find every normal file in your system, and then use the Xargs command to test what type of file they belong to

Command:

Find. -type F-print | Xargs file

Output:

[email protected] 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

[[Email protected] test]# find . -type f -print | xargs  file

./log2014.log: empty

./log2013.log: empty

./log2012.log: ascii  text

[[email protected] test]#

Example 2: Find the Memory information dump file (core dump) throughout the system and save the results to the/tmp/core.log file

Command:

Find/-name "core"-print | Xargs echo "" >/tmp/core.log

Output:

[Email protected] test]# Find/-name "core"-print | Xargs echo "" >/tmp/core.log

[Email protected] test]# cd/tmp

[email protected] 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 files with read, write, and execute permissions for all users in the current directory and reclaim the appropriate write permissions

Command:

Find. -perm-7-print | Xargs chmod o-w

Output:

[email protected] 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

[[email protected] test]# find. -perm-7-print | Xargs chmod o-w

[email protected] 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

[Email protected] test]#

Description

Permissions for the folder SCF, Test3, and test4 are changed after the command is executed

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

Command:

Find. -type F-print | Xargs grep "hostname"

Output:

[[email protected] test]# find. -type F-print | Xargs grep "hostname"

./log2013.log:hostnamebaidu=baidu.com

./log2013.log:hostnamesina=sina.com

./log2013.log:hostnames=true[[email Protected] test]#

Example 5: Use the grep command to search all normal files in the current directory for the word hostnames

Command:

Find. -name \*-type F-print | Xargs grep "Hostnames"

Output:

[[email protected] test]# find. -name \*-type F-print | Xargs grep "Hostnames"

./log2013.log:hostnamesina=sina.com

./log2013.log:hostnames=true[[email Protected] test]#

Description

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

Example 6: Performing MV with Xargs

Command:

Find. -name "*.log" | Xargs-i MV {} test4

Output:

[email protected] test]# LL

Total 316

-rw-r--r--1 root root 302108 11-03 06:19 log2012.log

-rw-r--r--1 root root 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

[Email protected] test]# CD test4/

[email protected] test4]# LL

Total 0[[email protected] test4]# CD.

[[email protected] test]# find. -name "*.log" | Xargs-i MV {} test4

[email protected] 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

[Email protected] test]# CD test4/

[email protected] test4]# LL

Total 304

-rw-r--r--1 root root 302108 11-12 22:54 log2012.log

-rw-r--r--1 root root 11-12 22:54 Log2013.log

-rw-r--r--1 root root 0 11-12 22:54 log2014.log

[Email protected] test4]#

Xargs prompt xargs:argument line too long solution after instance 7:find:

Command:

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

Output:

[[email protected] test4]# find. -type F-atime +0-print0 | xargs-0-l1-t rm-f

Rm-f

[[Email protected]]#

Description

-L1 is a processing of one;-T is the process of printing out a command before

Example 8: Use the-i parameter default front output with {} instead, the-i parameter can specify other substitution characters, as in the example []

Command:

Output:

[email protected] 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

[Email protected] test]# CD test4

[[email protected] test4]# find. -name "File" | Xargs-i [] CP []..

[email protected] test4]# LL

Total 304

-rw-r--r--1 root root 302108 11-12 22:54 log2012.log

-rw-r--r--1 root root 11-12 22:54 Log2013.log

-rw-r--r--1 root root 0 11-12 22:54 log2014.log

[Email protected] test4]# CD.

[email protected] test]# LL

Total 316

-rw-r--r--1 root root 302108 11-13 06:03 log2012.log

-rw-r--r--1 root root 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

[Email protected] test]#

Description

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

Use of the-p parameter for instance 9:xargs

Command:

Find. -name "*.log" | Xargs-p-i mv {}.

Output:

[email protected] test3]# LL

Total 0

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

[Email protected] test3]# CD.

[email protected] test]# LL

Total 316

-rw-r--r--1 root root 302108 11-13 06:03 log2012.log

-rw-r--r--1 root root 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

[Email protected] test]# CD TEST3

[[email protected] test3]# find. -name "*.log" | Xargs-p-i mv {}.

mv./log2015.log. ?... y

[email protected] test3]# LL

Total 0[[email protected] test3]# CD.

[email protected] test]# LL

Total 316

-rw-r--r--1 root root 302108 11-13 06:03 log2012.log

-rw-r--r--1 root root 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

[Email protected] test]#

Description

The-p parameter prompts you to confirm that the following command is executed, y executes, and N does not execute.

One Linux command per day: Xargs of the Find command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.