Common Linux commands (20)-find xargs
When you use the-exec option of the find command to process matched files, the find command passes all matching files to exec for execution. However, some systems have limits on the length of commands that can be passed to exec, so that an overflow error will occur after the find command runs for several minutes. The error message is usually "the parameter column is too long" or "parameter column overflow ". This is the use of the xargs command, especially used with the find command.
The find command passes the matching file to the xargs command, while the xargs command only obtains part of the file, not all, at a time, unlike the-exec option. In this way, it can first process the first part of the obtained files, then the next batch, and continue like this.
In some systems, the-exec option is used to initiate a corresponding process for processing each matching file, rather than executing all the matching files as parameters once; in some cases, there may be too many processes and the system performance may degrade, so the efficiency is not high. The xargs command has only one process. In addition, when using the xargs command, whether to obtain all parameters at a time or obtain parameters in batches, and the number of parameters obtained each time is determined based on the options of the command and the corresponding adjustable parameters in the system kernel.
Instance1:Search for every common file in the system, and then use the xargs command to test which files they belong.
Command: find.-type f-print | xargs file
Instance2:Search for the memory information dump file (core dump) in the system, and save the result to the/tmp/core. log file.
Command: find/-name "core"-print | xargs echo "">/tmp/core. log \
Instance3:Search for all files in the current directory with read, write, and execution permissions, and revoke the corresponding write permissions.
Command: find.-perm-7-print | xargs chmod o-w
Note: After the command is executed, the permissions of the scf, test3, and test4 folders are changed.
Instance 4:Use the grep command to search for the hostname word in all common files.
Command: find.-type f-print | xargs grep "hostname"
Instance 5:Use the grep command to search for hostnames in all common files in the current directory.
Command: find.-name \ *-type f-print | xargs grep "hostnames"
Instance6:Use xargs to execute music videos
Command: find.-name "*. log" | xargs-I mv {} test4
Instance7:After xargs is executed, the system prompts xargs: argument line too long. solution:
Command: find.-type f-atime + 0-print0 | xargs-0-l1-t rm-f
Note:-l1 is used to process one task at a time.-t is used to print out the command before processing.
Instance8:Use the-I parameter to replace the previous output with {} by default. The-I parameter can specify other replacement characters, as shown in [] in the example.
Command: find.-name "file" | xargs-I [] cp [] ..
Note: replace the previous output with {} by default with the-I parameter. You can specify other replacement characters for the-I parameter, as shown in [] in the example.
Instance9:Use of the-p Parameter of xargs
Command: find.-name "*. log" | xargs-p-I mv {}..
Note: The-p Parameter prompts you to confirm whether to execute the following command. Run y and run n.