简体   繁体   English

移动照片并重命名(如果存在)?

[英]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.

相关问题 使用Bash重命名和移动通配符文件 - Renaming and moving wildcard files with Bash 使用 crontab 重命名和移动文件 - Renaming and moving file using crontab 当生成的文件位于另一个文件系统上时,Java无法移动(重命名)文件 - Java fails in moving (renaming) a file when the resulting file is on another filesystem 用于在 Linux 上移动和重命名应用程序日志文件的 Bash 脚本 - Bash script for moving and renaming application log files on Linux 移动文件并在Bash上包含它的目录之后重命名它 - Moving a file and renaming it after the directory which contains it on Bash 以特定顺序重命名一组文件而不丢失文件(在Linux上) - Renaming a set of files in specific order without losing them (on Linux) 重命名文件后自动删除GIT存储库中的文件 - Automatically remove files in GIT repository after renaming them 重命名批量文件并更改某些前缀并将其稀疏 - Renaming Bulk files and changing certain prefix and raring them 使用“ mv”命令移动和重命名时,脚本会将其视为目录而不是新文件名 - When using “mv” command for moving and renaming, script sees it as a directory instead of new filename 如果文件已存在于新文件夹中,则使用空格移动文件并重命名 - Moving files with whitespaces and rename, if files already exist in the new folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM