如何从diff命令中仅提取文件名返回?

时间:2015-12-03 12:48:55

标签: linux bash grep diff

我正在尝试为同步2目录准备一个bash脚本。但是我无法从 diff 中归档名称。每次它转换为数组。

这是我的代码:

 #!/bin/bash
 DIRS1=`diff -r /opt/lampp/htdocs/scripts/dev/ /opt/lampp/htdocs/scripts/www/ `
 for DIR in $DIRS1
 do
    echo $DIR
 done

如果我运行这个脚本,我会把这样的东西拿出来:

Only
in
/opt/lampp/htdocs/scripts/www/:
file1
diff
-r
"/opt/lampp/htdocs/scripts/dev/File
1.txt"
"/opt/lampp/htdocs/scripts/www/File
1.txt"
0a1
>
sa
das
Only
in
/opt/lampp/htdocs/scripts/www/:
File
1.txt~
Only
in
/opt/lampp/htdocs/scripts/www/:
file
2
-
second

实际上我只想在找到差异的地方找到文件名,这样我就可以采取复制/删除方式。

由于

2 个答案:

答案 0 :(得分:1)

我不认为diff产生的输出可以很容易地为你的目的解析。通过迭代两个目录中的文件并在它们上运行diff,使用diff的返回值(并抛弃diff输出),可以解决您的问题。

执行此操作的代码有点长,但这里是:

DIR1=./one # set as required
DIR2=./two # set as required

# Process any files in $DIR1 only, or in both $DIR1 and $DIR2
find $DIR1 -type f -print0 | while read -d $'\0' -r file1; do
    relative_path=${file1#${DIR1}/};
    file2="$DIR2/$relative_path"
    if [[ ! -f "$file2" ]]; then
        echo "'$relative_path' in '$DIR1' only"
        # Do more stuff here
    elif diff -q "$file1" "$file2" >/dev/null; then
        echo "'$relative_path' same in '$DIR1' and '$DIR2'"
        # Do more stuff here
    else
        echo "'$relative_path' different between '$DIR1' and '$DIR2'"
        # Do more stuff here
    fi
done

# Process files in $DIR2 only
find $DIR2 -type f -print0 | while read -d $'\0' -r file2; do
    relative_path=${file2#${DIR2}/};
    file1="$DIR1/$relative_path"
    if [[ ! -f "$file2" ]]; then
        echo "'$relative_path' in '$DIR2 only'"
        # Do more stuff here
    fi
done

此代码利用一些技巧来安全地处理包含空格的文件,这很难通过解析diff输出来工作。您可以找到有关该主题的更多详细信息here

当然,对于内容相同但名称不同或位于不同目录的文件,这并没有做任何事情。

我通过填充两个测试目录进行测试,如下所示:

echo "dir one only" > "$DIR1/dir one only.txt"
echo "dir two only" > "$DIR2/dir two only.txt"
echo "in both, same" > $DIR1/"in both, same.txt"
echo "in both, same" > $DIR2/"in both, same.txt"
echo "in both, and different" > $DIR1/"in both, different.txt"
echo "in both, but different" > $DIR2/"in both, different.txt"

我的输出是:

'dir one only.txt' in './one' only
'in both, different.txt' different between './one' and './two'
'in both, same.txt' same in './one' and './two'

答案 1 :(得分:0)

使用-q标志并避免使用for循环:

diff -rq /opt/lampp/htdocs/scripts/dev/ /opt/lampp/htdocs/scripts/www/ 

如果您想要不同的文件:

diff -rq /opt/lampp/htdocs/scripts/dev/ /opt/lampp/htdocs/scripts/www/ |grep -Po '(?<=Files )\w+'|while read file; do
   echo $file
done
   -q  --brief
          Output only whether files differ.

但是,你应该检查rsynchttp://linux.die.net/man/1/rsync