Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's 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.
This example copies all text-files that have been found to the directory /mydir.
You will probably use mv instead of cp, but the principle is the same.
Please note that if /mydir is part of the searchpath of find, you will get error messages (or warnings) from cp (and probably mv) that source and destination are the same file.
The curly braces indicate to exec to use the result of the find operation.
Last edited by Wim Sturkenboom; 02-11-2005 at 04:42 AM.
Originally posted by Wim Sturkenboom
The curly braces indicate to exec to use the result of the find operation.
Could you please explain this a bit more, it's not the first time that I see this way of bash scripting and it's quite confusing because I haven't seen in no manual what so ever.
-exec command ;
Execute command; true if 0 status is returned. All
following arguments to find are taken to be argu_
ments to the command until an argument consisting
of `;' is encountered. The string `{}' is replaced
by the current file name being processed everywhere
it occurs in the arguments to the command, not just
in arguments where it is alone, as in some versions
of find. Both of these constructions might need to
be escaped (with a `\') or quoted to protect them
from expansion by the shell. The command is exe_
cuted in the starting directory.
I have found this topic on Google when searching for "bash move folder contents only" and I think I have a better solution, which saves CPU a lot, maybe Google will take someone here again in the future:
Code:
# not working version with too many files - this gives "/bin/mv: Argument list too long"
mv source/* target/
# too slow version with find and too many child processes
find source/ -exec mv {} target/ \;
# CPU nicer version - find your own limit instead of 1000
find source/ | xargs -L1000 mv -t target/
I have found this topic on Google when searching for "bash move folder contents only" and I think I have a better solution, which saves CPU a lot, maybe Google will take someone here again in the future:
Code:
# not working version with too many files - this gives "/bin/mv: Argument list too long"
mv source/* target/
# too slow version with find and too many child processes
find source/ -exec mv {} target/ \;
# CPU nicer version - find your own limit instead of 1000
find source/ | xargs -L1000 mv -t target/
Lot's of water have flowed in the river since this question was posted.
The optimal solution, based on your example would be
Code:
find source/ -exec mv '{}' target +
The plus sign causes find to use the maximum parameter limit allows on each mv invocation.
Lot's of water have flowed in the river since this question was posted.
...
The plus sign causes find to use the maximum parameter limit allows on each mv invocation.
...
Thanks for sharing this tip. My CentOS supports it. This makes find much better tool.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.