在具有重复文件名的目录中移动多个文件

谁能帮我这个?

我试图从我的USB复制图像到我的电脑上的档案,我决定做一个BASH脚本,使这项工作更容易。 我想复制文件(即IMG_0101.JPG),如果档案中已经存在一个具有该名称的文件(每当我使用它时将会有我擦的相机),那么这个文件应该命名为IMG_0101.JPG.JPG我不会丢失这个文件

#!/bin/bash if [ $# -eq 0 ] then echo "Usage: $0 image_path archive_path" exit 999 fi if [ -d "$1" ] #Checks if archive directory exists then echo Image Source directory FOUND else echo ERROR: Image Source directory has NOT BEEN FOUND fi if [ -d "$2" ] then echo Photo Archive FOUND else echo Creating directory mkdir "$2" fi if [ find $1 -name "IMG_[0-9][0-9][0-9][0-9].JPG" ] #added this in to be more specific 1/4 then #2/4 for file in "$1"/* do dupefile= "$2"/"$file" while [ -e "$newfile" ]; do newfile=$newfile.JPG done mv "$file" "$newfile" done else #3/4 #do nothing fi #4/4 took all the /4 out, but it's saying theres no such file or directory, even though I've tested it and it says there is. 

意外的令牌fi是我得到的错误,但if语句需要在那里,所以我需要的具体文件,正在移动。

你不能有空的else 。 如果你没有else部分, else关键字取出来。

另外,你应该摆脱多余的find 。 只是循环,而不是你真正想要的文件。

 for file in "$1"/IMG_[0-9][0-9][0-9][0-9].JPG do newfile= "$2"/"$file" while [ -e "$newfile" ]; do newfile=$newfile.JPG done mv "$file" "$newfile" done done 

(这也解决了dupefile vs newfile错误。)