小墨の博客

梦想需要付诸行动,否则只能是梦

linux如何查询文件及文件夹大小

linux下查询文件及文件夹大小不像windows那么简单,需要使用命令进行查看,本文将介绍如何查询文件大小、各文件夹大小及总大小统计方法。

假设有如下目录

ubuntu@10-23-189-141:~$ ll
total 96
drwxr-xr-x 10 ubuntu root    4096 Mar 31 13:02 ./
drwxr-xr-x  3 root   root    4096 May 16  2020 ../
-rw-------  1 ubuntu ubuntu  8482 Mar 31 00:04 .bash_history
-rw-r--r--  1 ubuntu root     220 Feb 25  2020 .bash_logout
-rw-r--r--  1 ubuntu root    3771 Feb 25  2020 .bashrc
drwx------  4 ubuntu root    4096 Mar 27 18:10 .cache/
drwx------  3 ubuntu root    4096 Mar 27 18:15 .config/
-rw-r--r--  1 ubuntu root      58 Mar 26 20:39 .gitconfig
drwx------  3 ubuntu root    4096 Mar 27 18:10 .local/
-rw-------  1 ubuntu root    1342 Mar 27 02:58 .mysql_history
drwxr-xr-x  4 ubuntu root    4096 Mar 26 23:34 .npm/
drwxr-xr-x  2 root   root    4096 Dec  6  2020 .pip/
-rw-r--r--  1 ubuntu root     807 Feb 25  2020 .profile
drwx------  2 ubuntu root    4096 Mar 26 23:04 .ssh/
-rw-r--r--  1 ubuntu root       0 May 16  2020 .sudo_as_admin_successful
drwxr-xr-x  2 ubuntu root    4096 Mar 31 13:02 test/
-rw-------  1 ubuntu root   13591 Mar 29 17:55 .viminfo
drwxr-xr-x  5 ubuntu root    4096 Mar 29 17:07 .vscode-server/
-rw-r--r--  1 ubuntu root     183 Mar 26 20:33 .wget-hsts
-rw-------  1 ubuntu root     229 Mar 31 12:58 .Xauthority


最简单的查看方法可以使用ls -ll、ls-lh命令进行查看,当使用ls -ll,会显示成字节大小,而ls- lh会以KB、MB等为单位进行显示,这样比较直观一些。

ubuntu@10-23-189-141:~$ ls -ll
total 4
drwxr-xr-x 2 ubuntu root 4096 Mar 31 13:02 test

ubuntu@10-23-189-141:~$ ls -lh
total 4.0K
drwxr-xr-x 2 ubuntu root 4.0K Mar 31 13:02 test


通过命令du -h --max-depth=1 *,可以查看当前目录下各文件、文件夹的大小,这个比较实用。

ubuntu@10-23-189-141:~$ du -h --max-depth=1 *
4.0K	test


查询当前目录总大小可以使用du -sh,其中s代表统计汇总的意思,即只输出一个总和大小。

同样,通过命令du -h --max-depth=0 *,可以只显示直接子目录文件及文件夹大小统计值。

如果只想查看指定目录的总大小,可以使用du -sh 目录名称。

ubuntu@10-23-189-141:~$ du -sh
181M	.

ubuntu@10-23-189-141:~$ du -h --max-depth=0 *
4.0K	test

ubuntu@10-23-189-141:~$ du -sh test
4.0K	test


对于指定文件夹也可以指定显示层次深度,如du -h --max-depth=0 software/及du -h --max-depth=1 software/

ubuntu@10-23-189-141:~$ du -h --max-depth=0 
181M	.

ubuntu@10-23-189-141:~$ du -h --max-depth=1
178M	./.vscode-server
1.0M	./.local
20K	./.ssh
4.0K	./test
1.7M	./.cache
636K	./.npm
8.0K	./.pip
8.0K	./.config
181M	.

ubuntu@10-23-189-141:~$ du -h --max-depth=2
172M	./.vscode-server/bin
4.1M	./.vscode-server/extensions
1.9M	./.vscode-server/data
178M	./.vscode-server
1020K	./.local/lib
1.0M	./.local
20K	./.ssh
4.0K	./test
312K	./.cache/pip
1.4M	./.cache/typescript
1.7M	./.cache
600K	./.npm/_cacache
32K	./.npm/_logs
636K	./.npm
8.0K	./.pip
4.0K	./.config/procps
8.0K	./.config
181M	.


查系统文件结构及使用大小用df -h,和实际文件大小是不一样的。

ubuntu@10-23-189-141:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            856M     0  856M   0% /dev
tmpfs           180M  1.1M  179M   1% /run
/dev/vda2        40G  8.1G   30G  22% /
tmpfs           898M     0  898M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           898M     0  898M   0% /sys/fs/cgroup
/dev/loop0       56M   56M     0 100% /snap/core18/2074
/dev/loop4       67M   67M     0 100% /snap/lxd/20840
/dev/vdb         20G  2.7G   18G  14% /data
/dev/loop7       56M   56M     0 100% /snap/core18/2344
/dev/loop8       62M   62M     0 100% /snap/core20/1376
/dev/loop1       44M   44M     0 100% /snap/snapd/15177
/dev/loop5       79M   79M     0 100% /snap/lxd/22754
/dev/loop3       62M   62M     0 100% /snap/core20/1405
tmpfs           180M     0  180M   0% /run/user/1000


张小弟之家

本文链接:
文章标题:

本站文章除注明转载/出处外,均为原创,若要转载请务必注明出处。转载后请将转载链接通过邮件告知我站,谢谢合作。本站邮箱:admin@only4.work

尊重他人劳动成果,共创和谐网络环境。点击版权声明查看本站相关条款。

    发表评论:

    搜索
    本文二维码
    标签列表
    站点信息
    • 文章总数:508
    • 页面总数:20
    • 分类总数:92
    • 标签总数:208
    • 评论总数:61
    • 浏览总数:225323

    | | |
    | |  Z-Blog PHP