Linux学习记录:查看文件和文件夹大小

总结一下在 Linux 下面如何查看 文件文件夹 的大小。为了方便理解和描述,我们的工作目录是 /datapool/workspace/tanjuntao/ ,所有的命令均在这个目录下面执行。

需求 1:如何查看 tanjuntao 下所有目录的大小,不显示子目录的大小

命令:

1
du -h --max-depth=1 .  # . 就表示当前目录,-h 表示 -human-readable,以 M、G 等方式显示大小

结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
user@DataServer:/datapool/workspace/tanjuntao$ du -h --max-depth=1 .
188K ./trash
1.5G ./WaveMsNet
449M ./FL-ESC10
5.7G ./FL-SVHN
236M ./pytorch-playground
1.7G ./ESC-50-master
30M ./stat_learn_project
648M ./pytorch-classification
109M ./vanilla_machine_learning
648M ./ml_fl
172M ./Federated-Learning-PyTorch
5.8G ./py3env
211M ./data
5.0M ./foolbox
1.7G ./adversarial-robustness-toolbox
43M ./archive
2.4G ./WaveMsNet-master
4.2G ./FL-Test
66G ./FL-Cifar10
1.5G ./pytorch-svhn
932M ./Environmental-Sound-Classification
115M ./python2
54M ./CBIR
384M ./pytorch-cifar
1.1G ./FL-Influence
189K ./federated-learning
427G ./LearnML
2.8G ./leaf
4.0K ./.vscode
8.0K ./test
92K ./learn_git
2.3G ./EvadeML-Zoo
196M ./AdvBox
114M ./Defensive-Distillation
530G .

注意最后一行,表示当前 tanjuntao 目录的大小是 530G。

如果我们还需要进一步查看 ./FL-Cifar10 目录下那些目录比较大,可以继续使用:

1
du -h --max-depth=1 ./FL-Cifar10

需求 2:如何只查看某个特定目录的大小

比方说我们想要查看 tanjuntao 这个目录的总大小,我们可以使用需求1 中提到的命令,最后找输出中的最后一行,就可以得到 tanjuntao 目录的总大小。但我们还有更简单的方式。

命令:

1
du -h --max-depth=0 .

结果:

1
2
user@DataServer:/datapool/workspace/tanjuntao$ du -h --max-depth=0 .
530G

也可以利用 -s 参数:

1
du -sh .  # -s 表示 -summarize

结果:

1
2
user@DataServer:/datapool/workspace/tanjuntao$ du -sh .
530G .

需求 3: 如何查看某个特定文件的大小

如果我们需要查看 tanjuntao 目录下 master.zip 文件的大小,可以这么做。

命令:

1
du -h ./master.zip

结果:

1
2
user@DataServer:/datapool/workspace/tanjuntao$ du -h ./master.zip
616M ./master.zip

除了使用 du 命令,还可以使用 ls

命令:

1
ls -lh ./master.zip  # -h 表示 -human-readable

结果:

1
2
user@DataServer:/datapool/workspace/tanjuntao$ ls -lh ./master.zip
-rw-rw-r-- 1 user user 616M Jan 31 20:31 ./master.zip

其中第五列就是文件大小。

需求 4: 如何查看当前目录下所有目录以及子目录的大小

如果我们需要查看 tanjuntao 目录下面所有目录的大小,包括子目录,可以这么做。

命令:

1
du -h . # 输出结果中只有目录,没有文件

需求 5: 如何查看某个目录下所有文件的大小

假如我们需要查看 tanjuntao/data 目录下所有文件的大小(输出结果中没有目录,只有文件),可以这么做。

命令:

1
du -ah ./data  # 输出结果中只有文件,没有目录

结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
user@DataServer:/datapool/workspace/tanjuntao$ du -ah ./data
59K ./data/raw/train-labels-idx1-ubyte
45M ./data/raw/train-images-idx3-ubyte
9.5K ./data/raw/t10k-labels-idx1-ubyte
7.5M ./data/raw/t10k-images-idx3-ubyte
53M ./data/raw
59K ./data/mnist/raw/train-labels-idx1-ubyte
45M ./data/mnist/raw/train-images-idx3-ubyte
9.5K ./data/mnist/raw/t10k-labels-idx1-ubyte
7.5M ./data/mnist/raw/t10k-images-idx3-ubyte
53M ./data/mnist/raw
7.6M ./data/mnist/processed/test.pt
46M ./data/mnist/processed/training.pt
53M ./data/mnist/processed
106M ./data/mnist
7.6M ./data/processed/test.pt
46M ./data/processed/training.pt
53M ./data/processed
211M ./data

需求 6:如何查看整个系统磁盘使用情况

命令:

1
df -h

结果:

1
2
3
4
5
6
7
8
9
10
11
user@DataServer:/datapool/workspace/tanjuntao$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 32G 0 32G 0% /dev
tmpfs 6.3G 562M 5.7G 9% /run
/dev/sda2 438G 40G 376G 10% /
tmpfs 32G 0 32G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/sda1 511M 3.4M 508M 1% /boot/efi
none 1.1P 32T 1.1P 3% /datapool
tmpfs 6.3G 0 6.3G 0% /run/user/2001

References

Donate comment here