Search code examples
shellcentosetl

Finding and Moving Duplicate Files from two Directories using shell script


Hey I have three folders

1 : Landing Folder
2 : Completed Folder
3 : Duplicates Folder

I want to compare the files in 1 & 2 for duplicates. If duplicates found I want to move the duplicate file to 3.

Is there any way of doing this using Shell Script?

What I've tried but to no success is using ls >file_names.txt on 1 & 2 to get their file names in text files, so that i can compare the textfiles for duplicate records. I get the list of duplicates in an echo command, cant figure out how to move them.


Solution

  • if file1 contains list of files in folder1 and file2 contains list of files in folder2 then below code will work

    grep -f file1 file2 | while read line
    do 
    mv folder1/$line folder3
    rm -f folder2/$line
    done