我希望运行一个Linux命令,它将递归地比较两个目录,并只输出不同目录的文件名。这包括在一个目录中而不在另一个目录中的任何内容,反之亦然,以及文本差异。
当前回答
您也可以使用rsync
rsync -rv --size-only --dry-run /my/source/ /my/dest/ > diff.out
其他回答
如果你想获取一个文件列表,这些文件只在一个目录中,而不是它们的子目录,只有它们的文件名:
diff -q /dir1 /dir2 | grep /dir1 | grep -E "^Only in*" | sed -n 's/[^:]*: //p'
如果你想递归列出所有的文件和目录,它们的完整路径是不同的:
diff -rq /dir1 /dir2 | grep -E "^Only in /dir1*" | sed -n 's/://p' | awk '{print $3"/"$4}'
这样就可以对所有文件应用不同的命令。
例如,我可以删除dir1而不是dir2中的所有文件和目录:
diff -rq /dir1 /dir2 | grep -E "^Only in /dir1*" | sed -n 's/://p' | awk '{print $3"/"$4}' xargs -I {} rm -r {}
我有一本目录。
$ tree dir1
dir1
├── a
│ └── 1.txt
├── b
│ └── 2.txt
└── c
├── 3.txt
├── 4.txt
└── d
└── 5.txt
4 directories, 5 files
我有另一个目录。
$ tree dir2
dir2
├── a
│ └── 1.txt
├── b
└── c
├── 3.txt
├── 5.txt
└── d
└── 5.txt
4 directories, 4 files
我可以区分两个目录。
$ diff <(cd dir1; find . -type f | sort) <(cd dir2; find . -type f| sort)
--- /dev/fd/11 2022-01-21 20:27:15.000000000 +0900
+++ /dev/fd/12 2022-01-21 20:27:15.000000000 +0900
@@ -1,5 +1,4 @@
./a/1.txt
-./b/2.txt
./c/3.txt
-./c/4.txt
+./c/5.txt
./c/d/5.txt
在我的linux系统上获取文件名
diff -q /dir1 /dir2|cut -f2 -d' '
rsync -rvc --delete --size-only --dry-run source dir target dir
从diff手册页:
-q只报告文件是否不同,而不报告差异的细节。 -r在比较目录时,递归地比较找到的任何子目录。
示例命令:
diff -qr dir1 dir2
示例输出(取决于地区):
$ 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
推荐文章
- Linux Bash中双&和分号有什么区别?
- 如何合并2 JSON对象从2个文件使用jq?
- 在SSH会话中查找客户端的IP地址
- C++ Linux的想法?
- 如何为Fedora安装g++ ?
- 如何触发命令行PHP脚本的XDebug分析器?
- Linux删除大小为0的文件
- 使用ImageMagick“Diff”图像
- Maven命令行如何指向特定的settings.xml为单个命令?
- Spring引导应用程序作为服务
- Git分支之间的视觉差异
- 如何重定向标准derr和标准输出到不同的文件在同一行脚本?
- Windows和Linux上的c++编译:ifdef开关
- Linux: kill后台任务
- 如何循环通过文件匹配通配符在批处理文件