Linux 查找存在于一个目录中但不在另一个目录中的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16787916/
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-06 23:03:20  来源:igfitidea点击:

Find the files existing in one directory but not in the other

linuxbashdiff

提问by Error404

I'm trying to find the files existing in one directory but not in the other, I tried to use this command:

我试图找到存在于一个目录中但不在另一个目录中的文件,我尝试使用以下命令:

diff -q dir1 dir2

The problem with the above command that it finds both the files in dir1but not in dir2as well as the files in dir2but not in dir1,

上面命令的问题是它同时找到文件 in dir1but not indir2以及文件 in dir2but not in dir1

I am trying to find the files in dir1but not in dir2only.

我试图找到文件,dir1但不仅dir2限于。

Here's a small sample of what my data looks like

这是我的数据的一个小样本

dir1    dir2    dir3
1.txt   1.txt   1.txt
2.txt   3.txt   3.txt
5.txt   4.txt   5.txt
6.txt   7.txt   8.txt

Another question on my mind is how can I find the files in dir1but not in dir2or dir3in a single command?

在我脑海里的另一个问题是,我怎么能找到的文件,dir1而不是在dir2dir3在一个单一的命令?

采纳答案by asclepix

diff -r dir1 dir2 | grep dir1 | awk '{print }' > difference1.txt

Explanation:

解释:

  • diff -r dir1 dir2shows which files are only in dir1 and those only in dir2 and also the changes of the files present in both directories if any.

  • diff -r dir1 dir2 | grep dir1shows which files are only in dir1

  • awkto print only filename.

  • diff -r dir1 dir2显示哪些文件仅在 dir1 中,哪些文件仅在 dir2 中,以及两个目录中存在的文件的更改(如果有)。

  • diff -r dir1 dir2 | grep dir1显示哪些文件只在 dir1 中

  • awk只打印文件名。

回答by plhn

comm -23 <(ls dir1 |sort) <(ls dir2|sort)

This command will give you files those are in dir1 and notin dir2.

此命令将为您提供 dir1 中而不是dir2 中的文件。

About <( )sign, you can google it as 'process substitution'.

关于<( )标志,你可以谷歌它作为“过程替换”。

回答by drrossum

vim's DirDiffplugin is another very useful tool for comparing directories.

vim 的DirDiff插件是另一个非常有用的比较目录的工具。

vim -c "DirDiff dir1 dir2"

It not only lists which files are different between the directories, but also allows you to inspect/modify with vimdiff the files that are different.

它不仅列出了目录之间不同的文件,而且还允许您使用 vimdiff 检查/修改不同的文件。

回答by tokhi

This should do the job:

这应该可以完成这项工作:

diff -rq dir1 dir2

Options explained (via diff(1) man page):

选项解释(通过 diff(1)手册页):

  • -r- Recursively compare any subdirectories found.
  • -q- Output only whether files differ.
  • -r- 递归比较找到的任何子目录。
  • -q- 仅输出文件是否不同。

回答by James

This is a bit late but may help someone. Not sure if diff or rsync spit out just filenames in a bare format like this. Thanks to plhn for giving that nice solution which I expanded upon below.

这有点晚了,但可能会帮助某人。不确定 diff 或 rsync 是否仅以这样的裸格式输出文件名。感谢 plhn 提供了我在下面扩展的很好的解决方案。

If you want just the filenames so it's easy to just copy the files you need in a clean format, you can use the find command.

如果您只需要文件名,以便以干净的格式轻松复制所需的文件,则可以使用 find 命令。

comm -23 <(find dir1 | sed 's/dir1/\//'| sort) <(find dir2 | sed 's/dir2/\//'| sort) | sed 's/^\//dir1/'

This assumes that both dir1 and dir2 are in the same parent folder. sed just removes the parent folder so you can compare apples with apples. The last sed just puts the dir1 name back.

这假设 dir1 和 dir2 都在同一个父文件夹中。sed 只是删除父文件夹,因此您可以将苹果与苹果进行比较。最后一个 sed 只是将 dir1 名称放回原处。

If you just want files:

如果你只想要文件:

comm -23 <(find dir1 -type f | sed 's/dir1/\//'| sort) <(find dir2 -type f | sed 's/dir2/\//'| sort) | sed 's/^\//dir1/'

Similarly for directories:

目录类似:

comm -23 <(find dir1 -type d | sed 's/dir1/\//'| sort) <(find dir2 -type d | sed 's/dir2/\//'| sort) | sed 's/^\//dir1/'

回答by jaltek

Another (maybe faster for large directories) approach:

另一种(对于大目录可能更快)方法:

$ find dir1 | sed 's,^[^/]*/,,' | sort > dir1.txt && find dir2 | sed 's,^[^/]*/,,' | sort > dir2.txt
$ diff dir1.txt dir2.txt

The sedcommand removes the first directory component thanks to Erik`s post)

由于 Erik 的帖子,sed命令删除了第一个目录组件)

回答by nerakk

A simplified way to compare 2 directories using the DIFF command

使用 DIFF 命令比较 2 个目录的简化方法

diff filename.1 filename.2 > filename.dat >>Enter

diff filename.1 filename.2 > filename.dat >>Enter

open filename.dat after the run is complete

运行完成后打开filename.dat

and you will see: Only in filename.1: filename.2 Only in: directory_name: name_of_file1 Only in: directory_Name: name_of_file2

您将看到:仅在 filename.1: filename.2 仅在:directory_name: name_of_file1 仅在:directory_Name: name_of_file2

回答by Catalin Hritcu

Meld (http://meldmerge.org/) does a great job at comparing directories and the files within.

Meld ( http://meldmerge.org/) 在比较目录和其中的文件方面做得很好。

Meld comparing directories

融合比较目录

回答by Aris

The accepted answer will also list the files that exist in both directories, but have different content. To list ONLY the files that exist in dir1 you can use:

接受的答案还将列出两个目录中都存在但内容不同的文件。要仅列出 dir1 中存在的文件,您可以使用:

diff -r dir1 dir2 | grep 'Only in' | grep dir1 | awk '{print }' > difference1.txt

Explanation:

解释:

  • diff -r dir1 dir2 : compare
  • grep 'Only in': get lines that contain 'Only in'
  • grep dir1 : get lines that contain dir
  • diff -r dir1 dir2 : 比较
  • grep 'Only in':获取包含 'Only in' 的行
  • grep dir1 : 获取包含 dir 的行

回答by Adail Junior

A good way to do this comparison is to use findwith md5sum, then a diff.

进行这种比较的一个好方法是使用findwith md5sum,然后使用a diff

Example:

例子:

Use findto list all the files in the directory then calculate the md5 hash for each file and pipe it to a file:

使用find到列表中的所有目录中的文件,然后计算出每个文件和管道到一个文件的MD5哈希:

find /dir1/ -type f -exec md5sum {} \; > dir1.txt

Do the same procedure to the another directory:

对另一个目录执行相同的步骤:

find /dir2/ -type f -exec md5sum {} \; > dir2.txt

Then compare the result two files with "diff":

然后用“diff”比较两个文件的结果:

diff dir1.txt dir2.txt

This strategy is very useful when the two directories to be compared are not in the same machine and you need to make sure that the files are equal in both directories.

当要比较的两个目录不在同一台机器上并且您需要确保两个目录中的文件相同时,此策略非常有用。

Another good way to do the job is using git

完成这项工作的另一个好方法是使用git

git diff --no-index dir1/ dir2/

Best regards!

此致!