Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 比较两个目录以生成输出_Bash_Unix_Diff - Fatal编程技术网

Bash 比较两个目录以生成输出

Bash 比较两个目录以生成输出,bash,unix,diff,Bash,Unix,Diff,我正在编写一个Bash脚本,将文件夹a(源)中的文件替换为文件夹B(目标)。但在此之前,我想录制两个文件 第一个文件将包含文件夹B中比文件夹a更新的文件列表,以及文件夹B中与文件夹a不同/孤立的文件 第二个文件将包含文件夹a中比文件夹B更新的文件列表,以及文件夹a中与文件夹B不同/孤立的文件 如何在Bash中实现这一点?我尝试使用diff-qr,但它会产生以下输出: Files old/VERSION and new/VERSION differ Files old/conf/mime.co

我正在编写一个Bash脚本,将文件夹a(源)中的文件替换为文件夹B(目标)。但在此之前,我想录制两个文件

  • 第一个文件将包含文件夹B中比文件夹a更新的文件列表,以及文件夹B中与文件夹a不同/孤立的文件
  • 第二个文件将包含文件夹a中比文件夹B更新的文件列表,以及文件夹a中与文件夹B不同/孤立的文件
如何在Bash中实现这一点?我尝试使用
diff-qr
,但它会产生以下输出:

Files old/VERSION and new/VERSION differ
Files old/conf/mime.conf and new/conf/mime.conf differ
Only in new/data/pages: playground
Files old/doku.php and new/doku.php differ
Files old/inc/auth.php and new/inc/auth.php differ
Files old/inc/lang/no/lang.php and new/inc/lang/no/lang.php differ
Files old/lib/plugins/acl/remote.php and new/lib/plugins/acl/remote.php differ
Files old/lib/plugins/authplain/auth.php and new/lib/plugins/authplain/auth.php differ
Files old/lib/plugins/usermanager/admin.php and new/lib/plugins/usermanager/admin.php differ
我也试过这个

(rsync -rcn --out-format="%n" old/ new/ && rsync -rcn --out-format="%n" new/ old/) | sort | uniq
但它没有给我所需要的结果范围。这里的问题是数据格式不正确,我只想在文本文件中显示文件而不是目录,例如:

conf/mime.conf
data/pages/playground/
data/pages/playground/playground.txt
doku.php
inc/auth.php
inc/lang/no/lang.php
lib/plugins/acl/remote.php
lib/plugins/authplain/auth.php
lib/plugins/usermanager/admin.php

目录B(
new/
)中比目录A(
old/
)新的文件列表:

这只是运行
find
并检查
new/
的内容,该内容由
-newerXY reference
过滤,其中
X
Y
都设置为
m
(修改时间),而
reference
本身就是
旧的
目录

目录B(
new/
)中缺少但目录A(
old/
)中存在的文件:

首先,这决定了输出文件,它要么来自第三个参数,要么来自其他输入,使用替换将斜杠转换为下划线(如果有路径),例如,以
bash script.bash/usr/local/bin/usr/bin
运行会将其文件列表输出到当前工作目录中的
\u usr\u local\u bin-newer-than-\u usr\u bin

这将组合这两个命令,然后确保对它们进行排序。不会有任何重复项,因此您无需担心(如果有,您将使用
sort-u


您可以通过在调用此脚本时更改参数顺序来获取第一个和第二个文件。

目录B(
new/
)中比目录A(
old/
)新的文件列表:

这只是运行
find
并检查
new/
的内容,该内容由
-newerXY reference
过滤,其中
X
Y
都设置为
m
(修改时间),而
reference
本身就是
旧的
目录

目录B(
new/
)中缺少但目录A(
old/
)中存在的文件:

首先,这决定了输出文件,它要么来自第三个参数,要么来自其他输入,使用替换将斜杠转换为下划线(如果有路径),例如,以
bash script.bash/usr/local/bin/usr/bin
运行会将其文件列表输出到当前工作目录中的
\u usr\u local\u bin-newer-than-\u usr\u bin

这将组合这两个命令,然后确保对它们进行排序。不会有任何重复项,因此您无需担心(如果有,您将使用
sort-u


您可以通过在调用此脚本时更改参数的顺序来获取第一个和第二个文件。

请参考Git bash更新Q的正文。请不要依赖其他评论来使问题描述完整/清晰。祝你好运。请参考Git bash更新Q的正文。请不要依赖其他评论来使问题描述完整/清晰。祝你好运。非常感谢。这非常有帮助!非常感谢,这非常有帮助!
find new -newermm old
A=old B=new
diff -u <(find "$B" |sed "s:$B::") <(find "$A" |sed "s:$A::") \
  |sed "/^+\//!d; s::$A/:"
#!/bin/bash
# Usage: bash script.bash OLD_DIR NEW_DIR [OUTPUT_FILE]
# compare given directories

if [ -n "$3" ]; then # the optional 3rd argument is the output file
  OUTPUT="$3"
else # if it isn't provided, escape path slashes to underscores
  OUTPUT="${2////_}-newer-than-${1////_}"
fi

{
  find "$2" -newermm "$1"
  diff -u <(find "$2" |sed "s:$2::") <(find "$1" |sed "s:$1::") \
    |sed "/^+\//!d; s::$1/:"
} |sort > "$OUTPUT"