2

I have quite a interesting problem and I've been struggling to find a solution. The situation is this:

I have two directories containing lots of files and folders (tens of thousands). Some of the files between those two directories are identical in size, but different in file names, file paths and content. I need to automatically find and replace same size files in the first directory with the ones from the second one, ignoring directory structure and file names of the second directory.

I've been trying to use fdupes as well as other similar tools, but they compare file contents, so it's not an option.

I've tried using gnome-search-tool to list all files and sort them by size, but replacing them manually is nuts as there are thousands of them.

I've been exploring solution presented in https://stackoverflow.com/questions/7541616/how-to-find-files-with-same-size, but none could fit my needs.

Could somebody point me to the right solution?

4
  • 2
    What should happen if there is more than one file of the same size in either (or both) directory? – steeldriver Dec 16 '20 at 17:42
  • 1
    @steeldriver In this case no action should be taken. It would be great to have a list of such files though. – Jonas S. Dec 16 '20 at 17:46
  • 1
    Yes this is an interesting problem. I'm sure it can be solved by a little bash script. There is probably a friendly soul here willing to knock up something for you. – Jos Dec 16 '20 at 17:51
  • 1
    This will generate a list sorted by size to get you started: find dir1 dir2 -type f -print0 | xargs -0 -r stat --format="%s %n" | sort -n. Detecting 2 (or more) and doing the right thing is left for you. – waltinator Dec 16 '20 at 22:13
0

Try this in a bash script file:

#!/bin/bash

dir1="/REPLACE/THIS/WITH/FULL/PATH/TO/dir1/"
dir2="/REPLACE/THIS/WITH/Full/PATH/TO/dir2/"

echo "Indexing $dir2 ... Please wait ..."

find "$dir2" -type f -follow -exec ls -l {} \; > "dir2_ls_file.txt"

while read f1 f2 f3 f4 f5 f6 f7 f8 f9
                do
                        size="${f5}"
                        name="${f9}"
                        result0=$(find "$dir1" -type f -size "$size"c -follow)
                        result1=$(echo "$result0" | wc -l)
                        result2=$(find "$dir2" -type f -size "$size"c -follow | wc -l)
                                if [ $result2 -gt 1 ]; then
                                        echo "There is more than one file under $dir2 with the same size as $name , so no action is taken!"
                                elif [ $result1 -eq 1 ] && [ "$result0" ]; then
                                        echo "$result0 is the same size as $name , so it will be replaced."
                                #       cp "$name" "$result0"
                                else
                                        echo "There is more than one file or no file under $dir1 with the same size as $name , so no action is taken!"
                                fi
                done < "dir2_ls_file.txt"

If you are satisfied with the result, un-comment cp "$name" "$result0" for the real copying to happen.

Please, first read here for more information and precautions.

0
0

I usually synchronise my Hard Drive with an External one, with tons of files and folders. If you don't want to mess up with Scripts, I recommend to use the super powerful and easy to use Freefilesync.

There are several ways to compare 2 folders, by name, size, content, date, etc.

https://freefilesync.org/

Download the version for Linux

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.