Prevent mv from overwriting the larger file by the same name

0

I need to move all files from a directory, discarding the directory structure.

Unfortunately, some files have the same file name (Thumbnails). I'm looking for smth. like this:

find /tmp/dirtree -type f -exec mv -i {} . \;

How can I prevent the overwriting of larger files during the process?

mate64

Posted 2012-10-26T10:40:48.793

Reputation: 177

Answers

2

Create the mymv.pl Perl script:

#!/usr/bin/perl
use File::Copy;
$file = shift;
($name = $file) =~ s#.*/##;
if (-f $name and -s $file < -s $name) {
     warn "$name not moved";
} else {
     move($file, $name);
}

Then, change the line to

find /tmp/dirtree -type f -exec perl mymv.pl {} \;

choroba

Posted 2012-10-26T10:40:48.793

Reputation: 14 741

This would probably get more upvotes if you explained what it did. At least from me. In the OP's question, it looks like he is moving the files to the current directory "." but I can't tell where you're moving it. – Buttle Butkus – 2016-04-07T03:19:15.510

Simple stupid solution means KISS! Please edit your post and add the perl parameter after -exec. I got curious issues without that. Thank you. – mate64 – 2012-10-26T13:35:31.170

@msec: Updated. – choroba – 2012-10-26T15:08:17.717