查找一个目录中存在的文件,而不是另一目录中的文件 [英] Find the files existing in one directory but not in the other

查看:71
本文介绍了查找一个目录中存在的文件,而不是另一目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

上述命令的问题是,它同时找到了dir1而不是dir2的文件以及dir2而不是dir1的文件

The problem with the above command that it finds both the files in dir1 but not in dir2 as well as the files in dir2 but not in dir1,

我正在尝试在dir1中查找文件,而不仅仅是在dir2中查找文件.

I am trying to find the files in dir1 but not in dir2 only.

这是我的数据的一小部分样本

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

我想到的另一个问题是,如何在单个命令中找到dir1中的文件,却找不到dir2dir3中的文件?

Another question on my mind is how can I find the files in dir1 but not in dir2 or dir3 in a single command?

推荐答案

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

说明:

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

  • diff -r dir1 dir2 shows 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 dir1显示哪些文件仅在目录1中

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

awk仅打印文件名.

这篇关于查找一个目录中存在的文件,而不是另一目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆