Linux Basics Command (xargs) Find command

Source: Internet
Author: User
Tags chmod ssh

When you use the-EXEC option of the Find command to process matching files, the Find command passes all matching files to exec execution. However, some systems have limits on the length of commands that can be passed to exec, so that an overflow error occurs several minutes after the find command has been run. The error message is usually either "too long" or "parameter column overflow." This is the use of the Xargs command, especially with the Find command.

The Find command passes the matching file to the Xargs command, and the Xargs command gets only a portion of the file at a time instead of all, unlike the-exec option. This allows it to process the first part of the file, then the next batch, and so on.

In some systems, using the-EXEC option initiates an appropriate process for processing each matching file, rather than executing all the matching files as parameters, so that in some cases there will be too many processes to degrade the performance of the system, and the use of the Xargs command is only a process. In addition, when using the Xargs command, whether to get all the parameters at once, or get the parameters in batches, and each time the number of parameters will be obtained according to the option of the command and the corresponding adjustable parameters in the system kernel.

Use instance:

Example 1: Find each common file in the system, and then use the Xargs command to test which types of files they belong to

Command:

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 0 11-12 22:25 log2013.log

-rw-r--r--1 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 save the results to the/tmp/core.log file

Command:

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 permissions in the current directory and reclaim the appropriate write permissions

Command:

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 0 11-12 22:25 log2013.log

-rw-r--r--1 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 0 11-12 22:25 log2013.log

-rw-r--r--1 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 are changed after the command has been executed

Example 4: Use the grep command to search for the word hostname in all normal 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]#

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.