I would like to move files from one folder to another but only if the destination file already exists and the sizes match. If it's not an exact filename and size match, I want to skip the move. Basically, I'm removing duplicates.



2 Answers 2


In it's core you have to write a simple script


for f in $src/*; do
  name=$(basename "$f")
  if [ -f "$dst/$name" ]; then
    src_size=$(stat --format '%s' "$f")
    dst_size=$(stat --format '%s' "$dst/$name")
    echo "$src_size $dst_size"
    # If the size match, check if the md5sum match, too
    # Then move of simply delete the src file

You should check the md5sum or sha*sum of both files, to be sure that they are really equal, or simply compare them.

  • Thanks. I ran into a problem because my src and dst both have spaces in their names. I used single and double quotes but basename can't handle the names. Oct 24, 2020 at 1:17
  • Nope. It's earlier than that. I've got src as "/home/name/first last" and if I throw in an echo $f right after the for statement, I get "/home/name/first". Oct 24, 2020 at 1:31
  • Nevermind. I need quotes around $src and $(basename "$f") to make it work. Thanks again. Oct 24, 2020 at 1:38

With rename (sometimes called prename or perl-rename to disambiguate from util-linux rename):

cd /one/folder &&
  rename '
    $other = "/another/folder/$_";
    $_ = $other if -e $other && -s $_ == -s $other' ./*

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .