文件夹大小和文件夹占用大小

工作中有需求,要对某些数据进行备份,所以罗列了需要备份后检查的方法,我知道的有四种,按照精确度进行如下排列:

du看占用空间大小 《 du-b文件字节数大小 《 diff对比 《 md5、sha等校验

测试文件如下:

E:	est>tree /F
│ 1.txt
│ 2.txt
│ 3.txt
│ 4.txt
│ 5.txt
│
├─111.txt
│ 2.txt
│ 3.txt
│ 4.txt
│
├─221.txt
│ 2.txt
│ 3.txt
│ 4.txt
│
├─331.txt
│ 2.txt
│ 3.txt
│ 4.txt
│
└─44
1.txt
2.txt
3.txt
4.txt

每个1-4.txt内文字为1-4,都是一个字符。

5.txt内为55,是2个字符。

现在对以上几种方法进行对比

1.du看文件夹大小

[root@localhost test]# du -a -h
4.0K    ./1.txt
4.0K    ./5.txt
4.0K    ./33/1.txt
4.0K    ./33/4.txt
4.0K    ./33/3.txt
4.0K    ./33/2.txt
20K     ./33
4.0K    ./44/1.txt
4.0K    ./44/4.txt
4.0K    ./44/3.txt
4.0K    ./44/2.txt
20K     ./44
4.0K    ./22/1.txt
4.0K    ./22/4.txt
4.0K    ./22/3.txt
4.0K    ./22/2.txt
20K     ./22
4.0K    ./4.txt
4.0K    ./3.txt
4.0K    ./2.txt
4.0K    ./11/1.txt
4.0K    ./11/4.txt
4.0K    ./11/3.txt
4.0K    ./11/2.txt
20K     ./11
104K    .

可以得出test文件夹总共占用了104K磁盘空间。

[root@localhost test]# ls -l |grep "^-"|wc -l    #统计当前文件夹下文件的个数,不包含子文件夹里的
5
[root@localhost test]# ls -l |grep "^d"|wc -l    #统计当前文件夹下目录的个数,不包含子文件夹里的
4
[root@localhost test]# ls -lR|grep "^-"|wc -l    #统计当前文件夹下文件的个数,包括子文件夹里的
2
[root@localhost test]# ls -lR|grep "^d"|wc -l    #统计文件夹下目录的个数,包括子文件夹里的
4

 可惜我的实际文件只有22字节,所以以上命令是占用的空间大小,ext4默认最小块为4k,所以一个1字节(1到4096字节)的文件占用空间为4k,4097字节占用空间为8k

不过如果在相同的文件系统中,占用空间应该是一致的,所以这是一种验证数据一致的方法,只是很嘈。

2.文件字节数大小

2.1 du -b

[root@localhost test]# du -ba
1       ./1.txt
2       ./5.txt
1       ./33/1.txt
1       ./33/4.txt
1       ./33/3.txt
1       ./33/2.txt
4100    ./33
1       ./44/1.txt
1       ./44/4.txt
1       ./44/3.txt
1       ./44/2.txt
4100    ./44
1       ./22/1.txt
1       ./22/4.txt
1       ./22/3.txt
1       ./22/2.txt
4100    ./22
1       ./4.txt
1       ./3.txt
1       ./2.txt
1       ./11/1.txt
1       ./11/4.txt
1       ./11/3.txt
1       ./11/2.txt
4100    ./11
20502   .
[root@localhost test]# du -ba
1       ./1.txt
2       ./5.txt
1       ./33/1.txt
1       ./33/4.txt
1       ./33/3.txt
1       ./33/2.txt
4100    ./33
1       ./44/1.txt
1       ./44/4.txt
1       ./44/3.txt
1       ./44/2.txt
4100    ./44
1       ./22/1.txt
1       ./22/4.txt
1       ./22/3.txt
1       ./22/2.txt
4100    ./22
1       ./4.txt
1       ./3.txt
1       ./2.txt
1       ./11/1.txt
1       ./11/4.txt
1       ./11/3.txt
1       ./11/2.txt
4100    ./11
20502   .

20502=test文件夹(4k)*1 + 4个子文件夹(4k)*4 + 20个文件(1bytes) + 5.txt(2bytes) = 20502字节

 2.2 winscp查看

通过winscp查看文件夹属性

这里看到的和du -b略有差别

  du -b看到是包含文件夹大小的字节数

  winscp看到是不包含文件夹大小的字节数

我常用winscp查看,因为和windows内的一致,方便在不同OS之间保持一致。其实两种都OK啦

3.diff查看 

diff -ruNaq ./11 ./22

diff比较也是很痛苦的,凡事一旦量大了,都痛苦。。。。

4.MD5查看

md5sum 1.txt    #单个文件的md5计算
find ./ -type f -print0 | xargs -0 md5sum > ./my.md5  #整个文件夹所有文件的md5计算

md5是我在实际工作中最经常用的校验方式,绝对保证数据一致,~~哈哈~~~

不过在遇到超大文件夹(TB级别)的时候md5好麻烦。。。

原文地址:https://www.cnblogs.com/tcicy/p/8295463.html