-1

In my previous question i was trying to push all my files inside folders and subfolders. Now i can with the command:

 find MainFolder -type f -exec mv \{\} FinalFolder \;

Now, i would like to avoid overwrite. Is it possible?

Thanks.

1

The mv recipe can overwrite files if you have two or more that have a name collision. If you want to avoid that you can get version numbers appended by using the cp command in place of mv.

find MainFolder -type f -exec cp --backup=numbered \{\} FinalFolder \;

Then you your duplicate files will have numbers in curly braces as version numbers appended to them if they collide

If cp keeps asking for confirmations, it might be because the cp command being aliased to cp -i fore saftey. A common initialization. In that case use /bin/cp in place of the cp

The original directory structure will still be there after running. If you don't want it. just rm -rf MainFolder it when you are sure you are done with it

1

The -n switch for mv is what you're after. From the manpage:

-n, --no-clobber do not overwrite an existing file

Baking this into your find script results in this:

find MainFolder -type f -exec mv -n \{\} FinalFolder \;
1
  • I believe this would make files be left behind in MainFolder if they duplicate a name – infixed Mar 17 '16 at 2:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.