linux两个文件夹之间的常用文件

时间:2016-01-13 07:46:28

标签: linux

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

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 :(得分:-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