[Tutor] How can I copy files recursively? [Off Topic, AGAIN]

زياد بن زياد بن
Tue Jul 11 06:21:47 CEST 2006


This is “Off Topic” regarding the mailing list!

On Mon, 2006-07-10 at 22:57 -0500, Dustin J. Mitchell wrote: (altered by
Ziyad)
> Richard Querin wrote:
> > ...
> > I was thinking I could do it easily from the linux command line
> > (cp -r copies the subfolders out as well) but I can't figure out
> > how to do it.
> > ...
> Python's not always the best with complex manipulations like that.  You
> could use os.path.walk, but here's the same thing in bash:
> 
>  find $IPODDER_DIR -type f -exec mv \{} $DEST_DIR \;
> 
> Dustin
You could use something like this:
        find SOURCE_DIR -type f -print0 | xargs -0 -i mv {} DIST_DIR

Where SOURCE_DIR is the top level directory holding the files and
DIST_DIR is the directory you want to move the files to.

Make sure there are no conflict in file names otherwise some files will
be overwritten!

Dustin's version will work perfectly but if there's a lot files it'll
exhaust your system because it will create a new process for each file
it finds (plus another process for the ‘find’ command itself), while the
other version will only create *three* processes only! (One for ‘find’,
another for ‘xargs’ and the third is for *one* ‘mv’ move command.)

(There's another way by using two piped ‘tar’ commands, but that's
overkilling it and the above solution is much more elegant.)

A side note:
        I apologise for the list as it seems I'm only good at posting
        anything *but* Python related.

Ziyad.



More information about the Tutor mailing list