linux - 在 rsync 之后,du 显示大小差异,而 diff 不显示

linux - after rsync, du shows size difference when diff does not

我使用 'rsync' 将一个大文件夹从 NTFS 复制到 ext4,并使用 'diff' 验证它。出于好奇,我还使用了 'du' 命令来检查文件夹是否具有相同的大小。虽然 'diff' 没有显示任何差异,但 'du' 显示文件夹有不同的大小。我在执行以下命令时没有遇到任何错误。

rsync --archive --recursive "$src" "$dest" 2>rsync_error.txt

sync

diff --brief --recursive --new-file "$src" "$dest" 1>diff-log.txt 2>diff-error.txt

然后我对每个文件夹使用'du':

du -sb "$src"
du -sb "$dest"
Output:
137197597476
137203512004

1.Why因为没有任何区别,会发生这种情况吗?

2.Should我担心我的数据或我的系统?

编辑: 我也试了'du -s --apparent-size'还是有区别

du 报告 space 包括文件系统 space,而不仅仅是文件内容大小。

同时检查可能不包含在您的 du 中的隐藏文件。

问候Invinciblecache,

谷歌搜索我发现了这个:

As du reports allocation space and not absolute file space, the amount of space on a file system shown by du may vary from that shown by df if files have been deleted but their blocks not yet freed. source

不是最好的来源,但很好地描述了 du 的用途。

所以,我依靠 diff 来检查文件的内容,但我建议忽略文件系统的大小差异,除非它太大,这不是这种情况。

稀疏文件

在 linux 下,您可以创建所谓的 sparse files。它们是完全 NULL 块并不真正存在的文件!

试试这个:

$ dd if=/dev/zero count=2048 of=normalfile
2048+0 records in
2048+0 records out
1048576 bytes (1.0 MB, 1.0 MiB) copied, 0.0103269 s, 102 MB/s

$ dd if=/dev/zero count=0 seek=2048 of=sparsefile
0+0 records in
0+0 records out
0 bytes copied, 0.000182708 s, 0.0 kB/s

然后

$ ls -l sparsefile normalfile
-rw-r--r-- 1 user  user  1048576 Feb  3 17:53 normalfile
-rw-r--r-- 1 user  user  1048576 Feb  3 17:53 sparsefile

$ du -b sparsefile normalfile
1048576     sparsefile
1048576     normalfile

但是

$ du -k sparsefile normalfile
0   sparsefile
1024        normalfile

$ du -h sparsefile normalfile
0   sparsefile
1.0M        normalfile

所以 sparsefile 中的 long 块没有被使用,它们将不会被 分配 !

$ du -k --apparent-size sparsefile normalfile
1024        sparsefile
1024        normalfile

然后

$ diff sparsefile normalfile
echo $?
0

两个文件几乎没有区别!

进一步

$ /sbin/mkfs.ext4 sparsefile 
mke2fs 1.44.5 (15-Dec-2018)
Filesystem too small for a journal
...
Writing superblocks and filesystem accounting information: done

$ ls -l sparsefile normalfile 
-rw-r--r-- 1 user  user  1048576 Feb  3 17:53 normalfile
-rw-r--r-- 1 user  user  1048576 Feb  3 17:59 sparsefile

$ du -k sparsefile 
32  sparsefile

$ diff sparsefile normalfile
Binary files sparsefile and normalfile differ