Linux BASH:合并两个目录并删除重复的数据

Linux BASH:合并两个目录并删除重复的数据,linux,bash,shell,Linux,Bash,Shell,我想比较两个文件夹的内容并删除重复的数据,实际上我编写了一个脚本(BASH),但我认为这不是正确的方法(我使用循环来迭代目录内容和大量diff命令,这会耗费太多时间) 我将解释上下文: 我有两个目录: 一,- 二,- 假设student1/homework1文件夹在dir1和dir2中包含相同的数据,而homework2包含不同的数据 输出目录应包含: Student1 homework1 //same name , sa

我想比较两个文件夹的内容并删除重复的数据,实际上我编写了一个脚本(BASH),但我认为这不是正确的方法(我使用循环来迭代目录内容和大量diff命令,这会耗费太多时间)

我将解释上下文:

我有两个目录:

一,-

二,-

假设student1/homework1文件夹在dir1和dir2中包含相同的数据,而homework2包含不同的数据

输出目录应包含:

       Student1
              homework1                 //same name , same content ==> keep one homework
              homework2
              homework2_dir2                //same name different content ==> _dir2

       Student2
              homework1 
              homework2 

       Student3
              homework1
              homework2
您认为在时间和可靠性(文件名问题等)方面进行此类操作的最佳方式是什么

谢谢;)

PS:dir*和Student*以及家庭作业*是目录

PS2:拜托,我不指望这个答案:

loop over student 
  loop over student homeworks
      test on homework existance
      diff on homework content
        if diff copy
  end
结束


如果我有很多学生和很多家庭作业,只有一个不同(只有一个作业不同),那么上面的解决方案会花费很多时间,我不确定它是否比你的解决方案快,因为你没有发布它

#!/bin/bash

mkdir output
cp -r dir1/* output

cd dir2
for student in Student* ; do
    (
        cd $student
        out_path=../../output/$student
        [[ -d $out_path ]] || mkdir $out_path
        for file in * ; do
            if [[ -f $out_path/$file ]] ; then
                diff -q $file $out_path/$file \
                    || cp $file $out_path/$file'_dir2'
            else
                cp $file $out_path/$student
            fi
        done
    )
done

假设dir1和dir2是没有目录的相对路径(即dir1或dir2中没有斜杠):

我没有试过这个-可能有一些拼写错误

在dir1中,递归到每个学生文件夹中,并在每个学生文件夹中递归到每个家庭作业目录中

在每个作业目录中,对每个文件使用
cmp
,检查其字节是否与dir2子树中的匹配文件相同

如果不同,在学生目录中创建一个备用作业目录,并将(
ln
)不同的文件链接到备用目录

cmp
diff
ln
cp


就这些,伙计们。

据我所知,您需要将两个不同目录中的所有文件合并到一个新目录中,而不需要重复的文件或文件夹

假设您要将它们合并到“合并”目录中

您可以这样做:

rsync -hrv /dir1 /merged/
rsync -hrv /dir2 /merged/

/dir1文件夹中的所有文件都将复制到/merged文件夹中,然后相同的过程将用于/dir2文件夹。

编写bash脚本以构建
git
repo。让
git
管理历史。@kev我不能在机器上安装git,我必须使用bash basic命令,但我很想知道如何使用git实现这一点,你能解释一下吗???它和我的差不多,(当我有很多学生和很多家庭作业时,这很耗时)@bachN:试着在
之后添加一个
&
。谢谢JezC,cmp用于文件(家庭作业是一个目录,包含很多文件),+1用于ln:)
#!/bin/bash

mkdir output
cp -r dir1/* output

cd dir2
for student in Student* ; do
    (
        cd $student
        out_path=../../output/$student
        [[ -d $out_path ]] || mkdir $out_path
        for file in * ; do
            if [[ -f $out_path/$file ]] ; then
                diff -q $file $out_path/$file \
                    || cp $file $out_path/$file'_dir2'
            else
                cp $file $out_path/$student
            fi
        done
    )
done
dir1=dir1
dir2=dir2
cd $dir1
BASEDIR=$(pwd)
for studentdir in *
cd $BASEDIR/$studentdir
do
  for homeworkdir in *
  cd $BASEDIR/$studentdir/$homeworkdir
  do
    for workfile in *
    do
      if cmp $workfile ${CMPDIR}/${studentdir}/${homeworkdir}/${workfile} 2>&1 >/dev/null
      then
        altdir=../${studentdir}_${dir2}
        mkdir ../${altdir}
        ln ${CMPDIR}/${studentdir}/${homeworkdir}/${workfile} ${altdir}
      fi
    done
  done
done
rsync -hrv /dir1 /merged/
rsync -hrv /dir2 /merged/