Difference between exec and xargs

Source: Internet
Author: User


What is the difference between exec and xargs? Both of them execute the Linux commands given to the files that meet the requirements without asking whether the user needs
Run this command. Www.2cto.com-exec :{} indicates that the command parameter is the file found, with; indicates the end of the comman command. \ Is an escape character,
Because the semicolon has its purpose in the command, a \ is used to indicate that this is a semicolon rather than other meanings. -OK: Same as-exec, the format is the same, but this parameter is executed in a safer mode.
A prompt will be provided before the shell command is given, asking the user to determine whether to execute the command. The xargs format must be combined with the pipeline: find [option] express | xargs command uses an instance to see how exec and xargs PASS Parameters: $ find test/-type ftest/myfile. nametest/files/role_filetest/files/install_file $ find test/-type f | xargs echotest/myfile. name test/files/role_file test/files/install_file $ find test/-type f-exec echo {}\; test/myfile. nametest/files/role_filetest/files/install_file is very obvious, exec is to execute a command for each found file, unless the single file name exceeds several k, otherwise not
An error is reported when the command line is too long. Xargs transfers all the file names to commands. When there are many files, these file names are combined
Line parameters can easily be too long, resulting in command errors. In addition, the combination of find | xargs will also encounter errors when processing file names with space characters, because the Command executed at this time
I do not know which are delimiter and which are spaces in the file name! This problem does not occur when exec is used. For example, to make a demo: $ touch test/'test zzh '$ find test/-name * zzhtest/test zzh $ find test/-name * zzh | xargs rmrm: cannot remove 'test/test': No such file or directoryrm: cannot remove 'zzh ': No such file or directory $ find test/-name * zzh-exec rm {}\; in contrast, it is not difficult to see their respective shortcomings 1. Every time exec processes a file or directory, it needs to start a command, which is not efficient; 2. the exec format is troublesome, you must use {} as the file replacement character and \; as the command Terminator, which is inconvenient to write. 3. xargs cannot operate on files with spaces in their file names. In summary, if the command to be used supports processing multiple files at a time, it is also known that there are no files with spaces in these files,
Therefore, it is easier to use xargs; otherwise, exec will be used.
 

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.