diff 只输出文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6217628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:00:20  来源:igfitidea点击:

diff to output only the file names

linuxcommand-linediff

提问by barfoon

I'm looking to run a Linux command that will recursively compare two directories and output onlythe file names of what is different. This includes anything that is present in one directory and not the other or vice versa, and text differences.

我希望运行一个 Linux 命令,该命令将递归比较两个目录并输出不同的文件名。这包括存在于一个目录中而不是另一个目录中的任何内容,反之亦然,以及文本差异。

采纳答案by John Kugelman

From the diff man page:

从差异手册页:

-q  Report only whether the files differ, not the details of the differences.
-r  When comparing directories, recursively compare any subdirectories found.

-q  仅报告文件是否不同,而不是差异的详细信息。
-r  比较目录时,递归比较找到的任何子目录。

Example command:

示例命令:

diff -qr dir1 dir2

Example output (depends on locale):

示例输出(取决于语言环境):

$ ls dir1 dir2
dir1:
same-file  different  only-1

dir2:
same-file  different  only-2
$ diff -qr dir1 dir2
Files dir1/different and dir2/different differ
Only in dir1: only-1
Only in dir2: only-2

回答by gerardw

On my linux system to get justthe filenames

在我的 linux 系统上获取文件名

diff -q /dir1 /dir2|cut -f2 -d' '

回答by N D

If you want to get a list of files that are only in one directory and not their sub directories and only their file names:

如果您想获取仅在一个目录中而不是在其子目录中且仅在其文件名中的文件列表:

diff -q /dir1 /dir2 | grep /dir1 | grep -E "^Only in*" | sed -n 's/[^:]*: //p'

If you want to recursively list all the files and directories that are different with their full paths:

如果要递归列出与其完整路径不同的所有文件和目录:

diff -rq /dir1 /dir2 | grep -E "^Only in /dir1*" | sed -n 's/://p' | awk '{print "/"}'

This way you can apply different commands to all the files.

通过这种方式,您可以对所有文件应用不同的命令。

For example I could remove all the files and directories that are in dir1 but not dir2:

例如,我可以删除 dir1 但不是 dir2 中的所有文件和目录:

diff -rq /dir1 /dir2 | grep -E "^Only in /dir1*" | sed -n 's/://p' | awk '{print "/"}' xargs -I {} rm -r {}

回答by boksiora

You can also use rsync

您也可以使用 rsync

rsync -rv --size-only --dry-run /my/source/ /my/dest/ > diff.out

回答by iolsmit

The approach of running diff -qr old/ new/has one major drawback: it may miss files in newly created directories. E.g. in the example below the file data/pages/playground/playground.txtis not in the output of diff -qr old/ new/whereas the directory data/pages/playground/is (search for playground.txtin your browser to quickly compare). I also posted the following solution on Unix & Linux Stack Exchange, but I'll copy it here as well:

运行的方法diff -qr old/ new/有一个主要缺点:它可能会丢失新创建的目录中的文件。例如,在下面的示例中,文件data/pages/playground/playground.txt不在输出中, diff -qr old/ new/而目录data/pages/playground/在(在浏览器中搜索Playground.txt以快速比较)。我还在Unix & Linux Stack Exchange 上发布了以下解决方案,但我也会在这里复制它:

To create a list of new or modified files programmatically the best solution I could come up with is using rsync, sort, and uniq:

要以编程方式创建新文件或修改文件的列表,我能想到的最佳解决方案是使用rsyncsortuniq

(rsync -rcn --out-format="%n" old/ new/ && rsync -rcn --out-format="%n" new/ old/) | sort | uniq

Let me explain with this example: we want to compare two dokuwiki releases to see which files were changed and which ones were newly created.

让我用这个例子来解释:我们想比较两个 dokuwiki 版本,看看哪些文件被更改了,哪些是新创建的。

We fetch the tars with wget and extract them into the directories old/and new/:

我们使用 wget 获取 tars 并将它们提取到目录中,old/然后new/

wget http://download.dokuwiki.org/src/dokuwiki/dokuwiki-2014-09-29d.tgz
wget http://download.dokuwiki.org/src/dokuwiki/dokuwiki-2014-09-29.tgz
mkdir old && tar xzf dokuwiki-2014-09-29.tgz -C old --strip-components=1
mkdir new && tar xzf dokuwiki-2014-09-29d.tgz -C new --strip-components=1

Running rsync one way might miss newly created files as the comparison of rsync and diff shows here:

以一种方式运行 rsync 可能会错过新创建的文件,因为 rsync 和 diff 的比较如下所示:

rsync -rcn --out-format="%n" old/ new/

yields the following output:

产生以下输出:

VERSION
doku.php
conf/mime.conf
inc/auth.php
inc/lang/no/lang.php
lib/plugins/acl/remote.php
lib/plugins/authplain/auth.php
lib/plugins/usermanager/admin.php

Running rsync only in one direction misses the newly created files and the other way round would miss deleted files, compare the output of diff:

仅在一个方向上运行 rsync 会错过新创建的文件,反之则会错过已删除的文件,比较 diff 的输出:

diff -qr old/ new/

yields the following output:

产生以下输出:

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

Running rsync both ways and sorting the output to remove duplicates reveals that the directory data/pages/playground/and the file data/pages/playground/playground.txtwere missed initially:

以两种方式运行 rsync 并对输出进行排序以删除重复项显示最初丢失了目录data/pages/playground/和文件data/pages/playground/playground.txt

(rsync -rcn --out-format="%n" old/ new/ && rsync -rcn --out-format="%n" new/ old/) | sort | uniq

yields the following output:

产生以下输出:

VERSION
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

rsyncis run with theses arguments:

rsync使用这些参数运行:

  • -rto "recurse into directories",
  • -cto also compare files of identical size and only "skip based on checksum, not mod-time & size",
  • -nto "perform a trial run with no changes made", and
  • --out-format="%n"to "output updates using the specified FORMAT", which is "%n" here for the file name only
  • -r“递归到目录”,
  • -c还比较相同大小的文件,并且仅“根据校验和跳过,而不是修改时间和大小”,
  • -n“在不做任何更改的情况下进行试运行”,以及
  • --out-format="%n"到“使用指定的格式输出更新”,此处为“%n”仅用于文件名

The output (list of files) of rsyncin both directions is combined and sorted using sort, and this sorted list is then condensed by removing all duplicates with uniq

rsync两个方向的输出(文件列表)使用 组合和排序sort,然后通过删除所有重复项来压缩此排序列表uniq

回答by mayank

rsync -rvc --delete --size-only --dry-run source dir target dir