MLUG: Re: [MLUG] copying many trees together
Re: [MLUG] copying many trees together
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
On Wed, 5 Sep 2007, Russell Horn wrote:

I was thinking if you have a lot of directories with foo as a subdirectory, you might want to use find like this:

#!/bin/bash
for i in `find ./ -maxdepth 2 -name 'foo'`
do
  cp -rp $i X/.
done

You might need to add this before the for loop:

export IFS="
"

Otherwise a directory name with a space in it could cause an error.

Can't this also be done with xargs in this kind of way?...

find . -maxdepth 2 -name foo -print0 | xargs -0 cp {} X/.

That avoids the for loop and the IFS problem. The use of -print0 and -0 options deals with the IFS issue by using null characters instead of whitespace as the field separator.

I didn't know about maxdepth, so thanks for telling me about that. I would have thought of that kind of approach if I had known that cp doesn't overwrite directories -- it just kinda meshes them together and it only overwrites files. It is funny that I didn't know such a thing given how much UNIX/Linux work I do, but the need to do that kind of copy doesn't come up very often for me. I was assuming that cp treats directories like it treats files -- overwriting when there are collisions unless -i is invoked.

By the way, I use -i aliases with mv and cp and recommend that to students, but I don't use the -i alias with rm because it becomes a habit to always say 'yes' and that defeats the purpose. Also, it creates an implicit expectation that "-i" is on, and that might cause the user to make a very big mistake on another system where -i is not on. It's different with mv and cp because the -i is only used in case of collisions, not with every invocation of the command as in rm.

Mike

_______________________________________________
members mailing list
EMAIL:PROTECTED
http://mlug.missouri.edu/mailman/listinfo/members