在目录中移动多个文件可能有重复的文件名 [英] Moving multiple files in directory that might have duplicate file names
问题描述
谁能帮助我?
我想从我的USB映像复制到我的电脑上存档,我决定做一个bash脚本,使这个工作更容易。我想复制文件(即IMG_0101.JPG),如果已经有处理档案的名称(其中还会有,因为我擦我的相机,每次我用它)的文件应命名为IMG_0101.JPG.JPG这样的文件我不丢失文件。
#method,然后
MV IMG_0101.JPG IMG_0101.JPG.JPG
否则MV IMG_0101路径/要/目的
在$源文件/ *;做
的newfile =$ DEST/文件$
而[-e$ newfile中];做
的newfile = $ newfile.JPG
DONE
CP$文件,$的newfile
DONE
有一个竞争条件在这里(如果另一个进程可以用第一个完成
和 CP $之间相同的名称创建一个文件C $ C>),但是这是相当的理论。
这不会是很难拿出一个不太原始更名的政策;也许替换 .JPG
在与越来越数字后缀结尾加上 .JPG
?
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 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.
#method, then
mv IMG_0101.JPG IMG_0101.JPG.JPG
else mv IMG_0101 path/to/destination
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
?
这篇关于在目录中移动多个文件可能有重复的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!