GoalMy goal is to merge directories. Whenever a file has the same name in two or more directories, then only the one that has the highest number of lines should be kept. If both files have the same number of files and differ then an error message should be thrown. Note that files having more lines are also bigger (in my specific case) which might be another way to compare files having the same name.My CodeHere is my code that I think works fine### Parameters ###GeneralPath=/Users/remi/Documents/Biologie/Vancouver/PhD/Thesis/BackgroundSelection/Simulations/s_and_Pi/outputs/4.0.2_1.0.5/cd ${GeneralPath} Directories=( HR OR OS ) # Array of directories ot be merged with the destination directoryDestination=HSerrorFile=${GeneralPath}MergeDirs.err### Do Stuff ###for d in ${Directories[@]};do    echo ${d}    cd $d    for f in *; do        echo       ${f}        if [ ! -f ../${Destination}/${f} ];then            echo cp1            cp ${f} ../${Destination}/        else            nblinesFrom=$(wc -l ${f} | awk -F  '{print $1}')            nblinesDest=$(wc -l ../${Destination}/${f} | awk -F  '{print $1}')            if [ ${nblinesFrom} -gt ${nblinesDest} ]; then                echo cp2                cp ${f} ../${Destination}/            elif [ ${nblinesDest} -gt ${nblinesFrom} ];then                echo Destination is bigger - nothing to do            else                DoTheyDiffer=$(diff ${f} ../${Destination}/${f} | wc -l)                if [ ${DoTheyDiffer} -gt 0 ];then                    echo ${f} and ../${Destination}/${f} diff but have the same number of lines >> ${errorFile}                fi            fi        fi    done    cd ..doneMy code seems quite complicated and I feel like a good combinaison of find -exec, awk, cp and diff might do something much more fancy.
Merging directories and keep files that have more lines
file;file system;bash