查找一个目录中存在的文件,而不是另一目录中的文件 [英] Find the files existing in one directory but not in the other
问题描述
我试图找到一个目录中存在的文件,但不在另一个目录中,我尝试使用此命令:
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
中的文件,却找不到dir2
或dir3
中的文件?
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屋!