移动在目录中的多个文件有重复的文件名 [英] Moving multiple files in directory that have duplicate file names

查看:424
本文介绍了移动在目录中的多个文件有重复的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮助我?

我想从我的USB映像复制到我的电脑上存档,我决定做一个bash脚本,使这个工作更容易。我想复制文件(即IMG_0101.JPG),如果已经有处理档案的名称(其中还会有,因为我擦我的相机,每次我用它)的文件应命名为IMG_0101.JPG.JPG这样的文件我不丢失文件。

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 to copy files(ie IMG_0101.JPG) and if there is already a file with that name in the archive (Which there will be as I wipe my camera everytime I use it) the file should be named IMG_0101.JPG.JPG so that I don't lose the file.

#!/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. 

意外的标记网络连接,我得到的错误,但if语句需要在那里,所以我需要的特定文件,越来越感动。

unexpected token fi is the error I'm getting but the if statement needs to be in there so the specific files i need, are getting moved.

推荐答案

您不能有一个空其他。取出其他关键字,如果你没有一个其他部分。

You cannot have an empty else. Take out the else keyword if you don't have an else part.

此外,你应该摆脱多余的找到。在你真正想要的,而不是只是文件循环。

Also, you should get rid of the superfluous find. Just loop over the files you actually want instead.

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 错误。)

这篇关于移动在目录中的多个文件有重复的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆