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.
Example 1: find each normal file in your system, and then use the Xargs command to test what type of file they belong to
command: F Ind.-type F-print | xargs file
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\
Example 3: finds all users with read, write, and execute permissions in the current directory and reclaims the appropriate write permissions
command: Find-perm-7-print | xargs chmod o-w
Description: The permissions of the folder SCF, Test3, and Test4 changed after the command was executed
Example 4: use the grep command to search all common files for the word hostname
command: Find-type f-print | xargs grep "hostname"
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"
Example 6: performing MV with Xargs
command: find.-name "*.log" | xargs-i mv {} test4
Example 7: find after executing xargs hint xargs:argument line too long solution:
command: find.-type f-atime +0-print0 | xargs-0-l1-t rm-f
Description: -l1 is a one-time process ; - T is the process before printing out the command
Example 8: Use the-i parameter to default the preceding output with {} instead, the-i parameter can specify other substitution characters, as in the example []
Command:find.-name "File" | xargs-i [] CP []..
Description: Use the-i parameter to default the preceding output with {} instead, the-i parameter can specify other substitution characters, as in the example []
Example 9: use of the-p parameter of the Xargs
command: find.-name "*.log" | xargs-p-i MV {}.
Description: The- p parameter prompts you to confirm that the following command is executed, y executes, and N does not execute.
Linux common Commands (20)-Xargs of Find