任何人都可以帮助我解决这个问题吗?在具有重复文件名的目录中移动多个文件
我想从我的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.
意外的标记网络连接,我得到的错误,但如果语句需要在那里,所以我需要的特定文件,越来越移动。
为什么不干脆前缀每everyfile与和索引号?或者你甚至可以用这个数字来替换整个名字,因为这个文件的名字对你来说并不重要。 –
你应该确保你*不会*用'mv'覆盖任何现有的文件,甚至不是由于脚本中的错误。为此只需将选项'-n'添加到'mv'。 – scai
你似乎混淆了'newfile'和'dupefile'。坚持一个或另一个。 – tripleee