bar*_*oon 228 linux diff command-line
我正在寻找一个Linux命令,它将递归地比较两个目录并仅输出不同的文件名.这包括一个目录中存在的任何内容而不是另一个目录中的任何内容,反之亦然,以及文本差异.
Joh*_*ica 346
从diff手册页:
-q
仅报告文件是否不同,而不是差异的详细信息.
-r
比较目录时,递归比较找到的所有子目录.
示例命令:
diff -qr dir1 dir2
Run Code Online (Sandbox Code Playgroud)
示例输出(取决于区域设置):
$ 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
Run Code Online (Sandbox Code Playgroud)
bok*_*ora 23
您也可以使用rsync
rsync -rv --size-only --dry-run /my/source/ /my/dest/ > diff.out
Run Code Online (Sandbox Code Playgroud)
N D*_*N D 13
如果要获取仅在一个目录而不是其子目录中的文件列表,并且只获取其文件名:
diff -q /dir1 /dir2 | grep /dir1 | grep -E "^Only in*" | sed -n 's/[^:]*: //p'
Run Code Online (Sandbox Code Playgroud)
如果要递归列出与其完整路径不同的所有文件和目录:
diff -rq /dir1 /dir2 | grep -E "^Only in /dir1*" | sed -n 's/://p' | awk '{print $3"/"$4}'
Run Code Online (Sandbox Code Playgroud)
这样,您可以对所有文件应用不同的命令.
例如,我可以删除dir1中的所有文件和目录,但不删除dir2:
diff -rq /dir1 /dir2 | grep -E "^Only in /dir1*" | sed -n 's/://p' | awk '{print $3"/"$4}' xargs -I {} rm -r {}
Run Code Online (Sandbox Code Playgroud)
在我的linux系统上只获取文件名
diff -q /dir1 /dir2|cut -f2 -d' '
Run Code Online (Sandbox Code Playgroud)
运行方法diff -qr old/ new/
有一个主要缺点:它可能会丢失新创建的目录中的文件.例如,在下面的示例中,文件data/pages/playground/playground.txt
不在输出中, diff -qr old/ new/
而目录data/pages/playground/
是(在浏览器中搜索playground.txt以快速比较).我还在Unix和Linux Stack Exchange上发布了以下解决方案,但我也将其复制到此处:
要以编程方式创建新文件或已修改文件的列表,我可以提出的最佳解决方案是使用rsync,sort和uniq:
(rsync -rcn --out-format="%n" old/ new/ && rsync -rcn --out-format="%n" new/ old/) | sort | uniq
Run Code Online (Sandbox Code Playgroud)
让我用这个例子来解释一下:我们想要比较两个dokuwiki版本,看看哪些文件被更改,哪些文件是新创建的.
我们取的焦油与wget和他们提取到的目录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
Run Code Online (Sandbox Code Playgroud)
以一种方式运行rsync可能会错过新创建的文件,因为rsync和diff的比较显示在此处:
rsync -rcn --out-format="%n" old/ new/
Run Code Online (Sandbox Code Playgroud)
产生以下输出:
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
Run Code Online (Sandbox Code Playgroud)
仅在一个方向上运行rsync错过了新创建的文件,反过来会错过已删除的文件,比较diff的输出:
diff -qr old/ new/
Run Code Online (Sandbox Code Playgroud)
产生以下输出:
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
Run Code Online (Sandbox Code Playgroud)
两种方式运行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
Run Code Online (Sandbox Code Playgroud)
产生以下输出:
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
Run Code Online (Sandbox Code Playgroud)
rsync
使用theses参数运行:
-r
"递归到目录", -c
还要比较相同大小的文件,只"基于校验和,不是模态时间和大小", -n
"进行试运行而不做任何改变",以及--out-format="%n"
"使用指定的FORMAT输出更新",此处仅为"%n",仅用于文件名使用rsync
两个方向的输出(文件列表)进行组合和排序sort
,然后通过删除所有重复项来压缩此排序列表uniq