How to check for duplicate files, if duplicates ar

2019-09-11 21:31发布

I am using a find command to find files formatted 'DGT_????.JPG'. I would like to then copy these to another folder. However, the script finds duplicate filenames and I am trying to find a way to append the files in the source directory being copied with an extra '.JPG' on the end of the filename instead of overwriting the file in the target directory. So for example if 'DGT_0001' was in both folders, the filename of the one in the source directory would be appended to 'DGT_0001.JPG.JPG', then moved to the target directory.

My code is below

#!/bin/sh
clear

SRC="$1"
DEST="$2"
SUFFIX=.JPG

if [ "$#" -eq 0 ]; then
    echo "two arguments required"
fi

if [ ! -d "$SRC" ]; then
    echo "Source directory does not exist"
    exit
fi

if [ ! -d "$DEST" ]; then
    mkdir "$2"
fi

for image in $(find "$SRC" -type f -iname IMG_[0-9][0-9][0-9][0-9].JPG)
do
    cp "$image" "$DEST"
done

标签: linux bash shell
1条回答
萌系小妹纸
2楼-- · 2019-09-11 22:16

You can have cp make the backup:

cp --backup --suffix=.JPG "$image" "$DEST"

From man cp:

--backup[=CONTROL]
       make a backup of each existing destination file

-S, --suffix=SUFFIX
       override the usual backup suffix
查看更多
登录 后发表回答