2016-01-13 47 views
-1

你好我有2个目录他们都包含目录 和文件的树。如果可能的话,我需要一个脚本来验证目录之间的公用文件 ,并且如果他找到一个公用文件,我需要在DIR2中删除它并建立到DIR1的链接。两个文件夹之间的linux常用文件

ex: DIR1 includes dir abc1 , abc2, abc3 and abc1 contains file a.txt DIR2 includes dir abc1 , abc4, and abc4 contains file a.txt Script should delete a.txt in DIR2/abc4 and make a link to DIR1/abc1/a.txt

该脚本将是优选为在bash,AWK,sed的或Perl。

谢谢!

+1

这看起来像一个面试问题。你在这里有编程问题吗?或者你只是要求有人为你做你的工作? –

+0

这不是一个面试问题,我只是不知道该怎么做,而我需要在我的脚本中。 – Marian

+0

find -f会给你一个目录路径中的文件列表。 xargs或者其他文件来散列文件。然后比较散列+文件路径列表。然后重复数据删除(取消链接文件,创建符号链接)。 –

回答

-1

创建目录

mkdir -p DIR1/abc{1,2,3} 
mkdir -p DIR2/abc{1,4} 

创建文件

touch DIR1/abc1/a.txt 
touch DIR2/abc4/a.txt 

获取所有的文件到临时文件

find DIR1 -name "*" -type f > DIR1.txt 
find DIR2 -name "*" -type f > DIR2.txt 

下面是脚本:

#!/bin/ksh 
cat DIR1.txt|while read LINE 
do 
    FILE_NAME=`echo $LINE|awk -F"/" '{ print $NF }'` 
    KEEP_FILE=$LINE 

    cat DIR2.txt|while read LINE2 
    do 
    FILE_NAME2=`echo $LINE2|awk -F"/" '{ print $NF }'` 
    if [ "${FILE_NAME}" == "${FILE_NAME2}" ]; then 
     echo `rm $LINE2` 
     ln -s `pwd`/$LINE $LINE2 
    fi 

    done 
done 
+0

非常感谢! – Marian