如何将两个目录的差异复制到新目录?

发布于 2024-10-21 15:41:40 字数 164 浏览 13 评论 0原文

假设我有两个目录:D1 和 D2。

D1(f1、f2、f3、f4) D2 (f1, f2)

我想将 D1 中不在 D2 中的文件复制到另一个目录 D3:

D3 (f3, f4)

在 Linux 中如何执行此操作?

谢谢, 三位一体

Suppose I have two directories : D1 and D2.

D1 ( f1, f2, f3, f4 )
D2 ( f1, f2 )

I want to copy the files in D1 , which are not in D2, to another directory D3 :

D3 ( f3, f4 )

How do I do this in linux ?

Thanx,
trinity

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

所有深爱都是秘密 2024-10-28 15:41:40

看看“dirdiff”包。它允许您做您想做的事情。

或者,这个 bash 命令行应该这样做:

for i in `ls D1` ; do if [ -f D2/$i ]; then echo "skip $i" ; else cp D1/$i D3 ; fi  done

注意 ls D1 周围的反引号 - 而不是单引号! (在美国键盘上,它位于〜(波形符)下方)

Look at the 'dirdiff'-package. It allows for what you want to do.

Alternatively, this bash command-line should do it:

for i in `ls D1` ; do if [ -f D2/$i ]; then echo "skip $i" ; else cp D1/$i D3 ; fi  done

Note the back-ticks around ls D1 - not single quotes! (On the US-keyboard it is beneath the ~(tilde))

旧街凉风 2024-10-28 15:41:40

从 D1 复制到 D3 时使用 -exclude 指定 D2
这将会完成...

while copying from D1 to D3 use -exclude specifying D2
and it would be done...

久伴你 2024-10-28 15:41:40

刚刚花了一天的大部分时间整理了类似的内容,并从一个旧的问题的答案中找到了答案。我最终得到了一个相当复杂的 bash 脚本:

#!/bin/bash

# setup folders for our different stages
DIST=/var/www/localhost/htdocs/dist/
DIST_OLD=/var/www/localhost/htdocs/dist_old/
DIST_UPGRADE=/var/www/localhost/htdocs/dist_upgrade/

cd $DIST

find . -type f | while read filename
do

    newfile=false
    modified=false
    if [ ! -e  "$DIST_OLD$filename" ]; then
        newfile=true
        echo "ADD $filename"
    elif ! cmp $filename $DIST_OLD$filename &>/dev/null; then
        modified=true
        echo "MOD $filename"
    fi

    if $newfile || $modified; then

        #massage the filepath to not include leading ./
        filepath=$DIST_UPGRADE$(echo $filename | cut -c3-)

        #create folder for it if it doesnt exist
        destfolder=$(echo $filepath | sed -e 's/\/[^\/]*$/\//')
        mkdir -p $destfolder

        #copy new/modified file to the upgrade folder
        cp $filename $filepath
    fi
done

Just spent a good portion of a day sorting out something similar and picked up from an answer of an older question. I ended up with a fairly elaborate bash script:

#!/bin/bash

# setup folders for our different stages
DIST=/var/www/localhost/htdocs/dist/
DIST_OLD=/var/www/localhost/htdocs/dist_old/
DIST_UPGRADE=/var/www/localhost/htdocs/dist_upgrade/

cd $DIST

find . -type f | while read filename
do

    newfile=false
    modified=false
    if [ ! -e  "$DIST_OLD$filename" ]; then
        newfile=true
        echo "ADD $filename"
    elif ! cmp $filename $DIST_OLD$filename &>/dev/null; then
        modified=true
        echo "MOD $filename"
    fi

    if $newfile || $modified; then

        #massage the filepath to not include leading ./
        filepath=$DIST_UPGRADE$(echo $filename | cut -c3-)

        #create folder for it if it doesnt exist
        destfolder=$(echo $filepath | sed -e 's/\/[^\/]*$/\//')
        mkdir -p $destfolder

        #copy new/modified file to the upgrade folder
        cp $filename $filepath
    fi
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文