Saturday, May 4, 2024
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 1355  / 1 Year ago, sat, november 19, 2022, 4:48:08

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?


More From » command-line

 Answers
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.


[#2211] Sunday, November 20, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zebrla

Total Points: 356
Total Questions: 110
Total Answers: 120

Location: Sudan
Member since Tue, Aug 3, 2021
3 Years ago
zebrla questions
Thu, Jul 15, 21, 14:09, 3 Years ago
Tue, Apr 19, 22, 10:58, 2 Years ago
Thu, Oct 21, 21, 02:00, 3 Years ago
Fri, Jul 1, 22, 01:16, 2 Years ago
Sat, Dec 11, 21, 22:05, 2 Years ago
;