[英]Moving photos and renaming them if they exist?
I'm trying to move photos from directories to one directory with find. 我正在尝试使用find将照片从目录移动到一个目录。 It works good:
它运作良好:
find /origin/path \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.JPG' -o -iname '*.JPEG' -o -iname '*.PNG' -o -iname '*.png' -o -iname '*.gif' -o -iname '*.GIF' \) -type f -exec mv -nv -t /final/path -- {} +;
How to rename files if they have the same name (but different photos)? 如果文件具有相同的名称(但不同的照片),如何重命名文件?
You can use the --backup=t
option for mv. 您可以对mv使用
--backup=t
选项。 This will append an increasing numbered suffix to files whose target already exists. 这将为目标已存在的文件附加增加编号的后缀。
$ find /tmp/test -type f
/tmp/test/dir2/image.jpg
/tmp/test/dir3/image.jpg
/tmp/test/dir1/image.jpg
/tmp/test/dir4/image.jpg
$ mkdir /tmp/test2
$ find /tmp/test -iname '*.jpg' -print0 | xargs -0 mv -v --backup=t --target-directory=/tmp/test2
‘/tmp/test/dir2/image.jpg’ -> ‘/tmp/test2/image.jpg’
‘/tmp/test/dir3/image.jpg’ -> ‘/tmp/test2/image.jpg’ (backup: ‘/tmp/test2/image.jpg.~1~’)
‘/tmp/test/dir1/image.jpg’ -> ‘/tmp/test2/image.jpg’ (backup: ‘/tmp/test2/image.jpg.~2~’)
‘/tmp/test/dir4/image.jpg’ -> ‘/tmp/test2/image.jpg’ (backup: ‘/tmp/test2/image.jpg.~3~’)
$
sorry i didn't because i'm on windows now, but below script should do it 对不起,我没有,因为我现在在Windows上,但在脚本下面应该这样做
files_list=$(find /origin/path \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.JPG' -o -iname '*.JPEG' -o -iname '*.PNG' -o -iname '*.png' -o -iname '*.gif' -o -iname '*.GIF' \) -type f)
for file in ${files_list}
do
counter=0
while true
do
if [[ ! -a ${file} ]]
then
mv "${file}" "/final/path/${file}"
break
else
file="${file}${counter}"
(( counter++ ))
fi
done
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.