从特定点复制树目录

从特定点复制树目录

我现在知道如何表达这一点,但是:

我正在将所有最新的 .zip 文件复制到此原始文件夹内的每个递归子目录:

"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Original"

例如

"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Original/test1/zip11.zip"
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Original/test1/zip12.zip"
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Original/test2/zip21.zip"
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Original/test2/test3/zip31.zip"
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Original/test2/test3/zip32.zip"
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Original/test2/test3/zip33.zip"

这会将 zip12.zip、zip21.zip、zip33.zip(这些是每个目录中的最新文件)复制到备份文件夹。

我已经完成了这个工作,所以所有这些都复制到这个目录:

"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Backup"

所以它看起来像:

"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Backup/zip12.zip"
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Backup/zip21.zip"
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Backup/zip33.zip"

我希望它使用相同的树结构(从原始版本开始),所以它看起来像:

"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Backup/test1/zip12.zip"
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Backup/test2/zip21.zip"
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Backup/test2/test3/zip33.zip"

有人能指出我正确的方向吗?

这是我的代码:

#THE DIRECTORY THE ORIGINAL FILE IS STORED IN
MYDIR="/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Original"

#THE DIRECTORY THE BACKUP FILE WILL BE STORED IN
DEST="/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Backup1/"

#FIND ALL DIRECTORIES AND NAME TEMP DIRECTORY AS CURRENT DIRECTORY
find "${MYDIR}" -type d | while read CURRENTDIRECTORY;
    do 

        #FINDS THE NEWEST ZIP FILE IN T HE DIRECTORY
        NEWESTFILE=`ls -dtr1 "${CURRENTDIRECTORY}"/*.zip | tail -n 1`
        echo "NF = $NEWESTFILE"

        #CHECK IF FILE IS PRESENT
        if [ -z "${NEWESTFILE}" ] ; then
            echo "No file to copy"

        #CHECK IF FILE IS A DIRECTORY
        elif [ -d "${NEWESTFILE}" ] ; then
            echo "Newest file is a directory"

        #COPY NEWEST FILE TO DEST
        else
            echo "Copying ${NEWESTFILE} --> ${DEST}"
            cp -p "${NEWESTFILE}" "${DEST}"
        fi

    done

答案1

您可以使用rsync而不是cp

rsync -R "${CURRENTDIRECTORY}"/"${NEWESTFILE}" "${DEST}"

要将输出路径限制为相对于MYDIR(例如test1/zip12.zip),您必须在 find Loop: 之前cd $MYDIR和之后输入目录find .

如果您将旧文件保留在备份目录中,您甚至可以使用rsync --ignore-existing.它只会复制新文件。

阅读更多内容人同步

相关内容