Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
find with exec is a much more efficient way of doing it. There is a finite limt to the length of arguments passed to cp and if you have a LOT of files that match the criteria then you could exceed that limit. With find/exec this isn't an issue as each file is done "individually".
The "+" terminator indicates that cp can accept multiple arguments, and find will invoke it with an arg list up to maximum length and use multiple invocations only if the number of arguments would exceed that. Note that use of the "-t" (--target-directory) option is required because the multiple file name arguments must be last in the argument list.
Note that these solutions do not replicate the original directory tree. All the files go directly into the target directory.
[sarcasm]what idiot puts white space in filenames[/sarcasm]
is it really faster, i assume invoking find, xargs and cp would slow things down a tad ?
Nope, it would be faster and safer than all that has been posted so far, because xargs gathers the arguments and passes them to 'cp' in batches. If you pass them one at a time as find does, it will be slower. However, if you use variable expansion like in your second example, white space will not be handled properly.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.