Linux中两个目录的区别[关闭]

本文翻译自:Difference between two directories in Linux [closed]

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 dir1 but not in dir2 as well as the files in dir2 but not in dir1 , 上面命令的问题是,它在dir1找到了文件,但在dir2找不到文件,在dir2找不到文件但在dir1找不到,

I am trying to find the files in dir1 but not in dir2 only. 我试图在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 dir1 but not in dir2 or dir3 in a single command? 我想到的另一个问题是如何在单个命令中找到dir1的文件而不是dir2dir3中的文件?


#1楼

参考:https://stackoom.com/question/18riQ/Linux中两个目录的区别-关闭


#2楼

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

Explanation: 说明:

  • 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显示哪些文件仅在dir1中,哪些文件仅在dir2中,以及两个目录中存在的文件的更改(如果有)。

  • diff -r dir1 dir2 | grep dir1 diff -r dir1 dir2 | grep dir1 shows which files are only in dir1 diff -r dir1 dir2 | grep dir1显示哪些文件仅在dir1中

  • awk to print only filename. awk只打印文件名。


#3楼

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

This command will give you files those are in dir1 and not in dir2. 此命令将为您提供dir1中的文件,而不是 dir2中的文件。

About <( ) sign, you can google it as 'process substitution'. 关于<( )符号,您可以将其视为“流程替换”。


#4楼

vim's DirDiff plugin 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检查/修改不同的文件。


#5楼

This should do the job: 这应该做的工作:

diff -rq dir1 dir2

Options explained (via diff(1) man page ): 解释选项(通过diff(1) 手册页 ):

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

#6楼

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. 不确定diff或rsync是否只是以像这样的裸格式吐出文件名。 Thanks to plhn for giving that nice solution which I expanded upon below. 感谢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. 这假设dir1和dir2都在同一个父文件夹中。 sed just removes the parent folder so you can compare apples with apples. sed只删除父文件夹,以便您可以将苹果与苹果进行比较。 The last sed just puts the dir1 name back. 最后一个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/'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值