Moving multiple files in directory that might have duplicate file names

前端 未结 2 605
滥情空心
滥情空心 2021-01-27 00:31

can anyone help me with this?

I am trying to copy images from my USB to an archive on my computer, I have decided to make a BASH script to make this job easier. I want t

2条回答
  •  天命终不由人
    2021-01-27 00:37

    for file in "$source"/*; do
        newfile="$dest"/"$file"
        while [ -e "$newfile" ]; do
            newfile=$newfile.JPG
        done
        cp "$file" "$newfile"
    done
    

    There is a race condition here (if another process could create a file by the same name between the first done and the cp) but that's fairly theoretical.

    It would not be hard to come up with a less primitive renaming policy; perhaps replace .JPG at the end with an increasing numeric suffix plus .JPG?

提交回复
热议问题