简体   繁体   English

查找和替换两个文件夹之间的图像文件

[英]Find and Replace Image Files Between Two Folders

I have two folders on my Mac's hard drive. Mac的硬盘上有两个文件夹。

Directory A, and Directory B 目录A和目录B

Both directories contain image files. 这两个目录都包含图像文件。

I need to find matching filenames is Dir A. 我需要找到匹配的文件名是DirA。

If I find a match overwrite the file in Dir A with the matching one from Dir B. 如果找到匹配项,则用Dir B中的匹配项覆盖Dir A中的文件。

How should I approach this? 我应该如何处理?

rsync is THE best tool for synchronizing files. rsync的是用于同步文件最佳工具。 It has tons of options so be sure to read the man page in detail. 它有很多选项,因此请务必仔细阅读手册页。

rsync -rv --inplace --existing /path/to/dir/b/* /path/to/dir/a

If you want to write a script then the this should do what you are looking for: 如果您想编写脚本,那么这应该可以满足您的需求:

#!/bin/bash

DIR_A='/path/to/dir_a'
DIR_B='/path/to/dir_b'

for file in "$DIR_B"/*; do 
    name="${file##*/}" 
    if [[ -e $DIR_A/$name ]]; then 
        echo "Match found = $name"; 
        cp "$file" "$DIR_A" 
    fi
done

What it does is, look for files in Directory B and extracts the file name (since we use absolute paths). 它的作用是,在目录B中查找文件并提取文件名(因为我们使用绝对路径)。 It checks for that filename to see if it exists in Directory A. -e is the test that does that. 它检查该文件名以查看目录A中是否存在该文件名。- -e是执行该操作的测试。 If it is successful test then we print a message saying that Match found along with the filename. 如果测试成功,那么我们将显示一条消息,指出Match found和文件名。 We then proceed to copy the file from Directory B to Directory A. 然后,我们将文件从目录B复制到目录A。

Now you may choose to remove the message that prints out to the screen and use mv instead of cp if you don't want the copy present in Directory B. 现在,如果您不希望副本出现在目录B中,则可以选择删除打印到屏幕上的消息,并使用mv而不是cp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM