in reply to Optimizing files' moving with File::Find & File::Copy modules

If $dir2 doesn't contain any directories, you could just do:
system "mv $dir1/* $dir2";
which does work even if there are filenames with spaces in them.

If $dir2 contains directories, you could do:

system "find $dir1 -type f -exec mv {} $dir2 ';'";
Note that File::Copy::move doesn't allow moving more than one file at a time.

Replies are listed 'Best First'.
Re^2: Optimizing files' moving with File::Find & File::Copy modules
by Anonymous Monk on Nov 17, 2009 at 02:52 UTC
    Thanks for your suggestions.

    I could have used system "find ...", but I'd be facing the same issues ( special chars, whitespaces in filenames ), and also I'd like to avoid find ... -exec .. as this will process the files one by one. I need find, cause I need a recursive selective check to be done on all files.

    >> Note that File::Copy::move doesn't allow moving more than one file at a time.<<

    Hmmm ... that's bad news ... :(

    My whole point is optimization, so having _all_ the files ( with potentially bad characters in their names ) available to 'mv' command, but I guess that's not possible, and doing that with shell only is a real pain.

    Thank you anyway.
      system "find $dir1 -type f -exec mv -t $dir2 '{}' '+'";
      Spaces and special characters aren't a problem as find doesn't invoke a shell. And by using a + instead of a ;, the -exec will behave as xargs.

      You may need GNU implementations of find and mv, I do not know whether mv -t is a POSIX requirement, nor do I know that of find -exec command . But I doubt it's much of a problem to get those GNU tools running on a platform that supports perl.

        If he doesn't have that implementation of find, and there are spaces in filenames, the array form of system will handle them:
        my @files = <$dir1/*>; system("mv", @files, $dir2);