如何在Linux中查看文件和目录的大小?使用df -m
命令,可以显示顶层目录下所有目录的大小,但是对于目录和文件内部的内容,如何检查它们的大小呢?
使用ls
命令查看文件大小,使用du
命令查看目录大小。
检查文件大小
ls -l filename #Displays Size of the specified file
ls -l * #Displays Size of All the files in the current directory
ls -al * #Displays Size of All the files including hidden files in the current directory
ls -al dir/ #Displays Size of All the files including hidden files in the 'dir' directory
ls
命令将不会列出目录的实际大小(为什么?)。因此,我们使用 du
进行此操作。
检查目录大小
du -sh directory_name #Gives you the summarized(-s) size of the directory in human readable(-h) format
du -bsh * #Gives you the apparent(-b) summarized(-s) size of all the files and directories in the current directory in human readable(-h) format
在上述任何一个命令中加入-h
选项(例如:ls -lh *
或du -sh
)将以人类可读的格式显示大小(kb
,mb
,gb
等)。
更多信息请参见man ls
和man du
有 du
命令。
整个目录和/或文件的大小(-s
),多个参数的总计(-c
),以人类友好的方式显示(-h
):
$ du -sch ~/.bashrc /tmp $XDG_RUNTIME_DIR
8.0K /home/max/.bashrc
358M /tmp
80K /run/user/1000
359M total
--apparent-size
命令行开关使其测量表面大小(即ls
显示的大小),而不是实际磁盘使用量。
-h
标志应该能够实现你所要求的功能:以人类可读的格式打印大小(例如,1K 234M 2G)。 - Maxim Egorushkin--apparent-size
。 - Maxim Egorushkindu -hs *
命令来查看当前目录中所有文件和文件夹的大小。 - Jordan Stewart使用ls -s
列出文件大小,或者如果您更喜欢,可以使用ls -sh
以人类可读的方式显示文件大小。
对于目录,请使用du
,同样,使用du -h
以人类可读的方式显示文件大小。
还有一个很好的ncdu
实用工具-它可以显示目录大小以及子文件夹和文件的详细信息。
Ubuntu:
$ sudo apt-get install ncdu
在命令行中输入ncdu [路径]
。等待几秒钟进行路径分析后,您将看到类似以下内容:
$ ncdu 1.11 ~ Use the arrow keys to navigate, press ? for help
--- / ---------------------------------------------------------
. 96,1 GiB [##########] /home
. 17,7 GiB [# ] /usr
. 4,5 GiB [ ] /var
1,1 GiB [ ] /lib
732,1 MiB [ ] /opt
. 275,6 MiB [ ] /boot
198,0 MiB [ ] /storage
. 153,5 MiB [ ] /run
. 16,6 MiB [ ] /etc
13,5 MiB [ ] /bin
11,3 MiB [ ] /sbin
. 8,8 MiB [ ] /tmp
. 2,2 MiB [ ] /dev
! 16,0 KiB [ ] /lost+found
8,0 KiB [ ] /media
8,0 KiB [ ] /snap
4,0 KiB [ ] /lib64
e 4,0 KiB [ ] /srv
! 4,0 KiB [ ] /root
e 4,0 KiB [ ] /mnt
e 4,0 KiB [ ] /cdrom
. 0,0 B [ ] /proc
. 0,0 B [ ] /sys
@ 0,0 B [ ] initrd.img.old
@ 0,0 B [ ] initrd.img
@ 0,0 B [ ] vmlinuz.old
@ 0,0 B [ ] vmlinuz
按下 d 键删除当前高亮元素,按下 CTRL + c 退出
ls -lh
前往选择的目录并执行:
$ du -d 1 -h
其中:
-d 1 is the depth of the directories
-h is the human-readable option
您将看到这样:
0 ./proc
8.5M ./run
0 ./sys
56M ./etc
12G ./root
33G ./var
23M ./tmp
3.2G ./usr
154M ./boot
26G ./home
0 ./media
0 ./mnt
421M ./opt
0 ./srv
2.6G ./backups
80G .
文件大小(以MB为单位)
ls -l --b=M filename | cut -d " " -f5
文件大小(以GB为单位)
ls -l --b=G filename | cut -d " " -f5
使用命令 ls -l --block-size=M 可以给出长格式列表(必须这样才能看到文件大小)并将文件大小四舍五入到最近的 MiB。
如果您想要 MB(10^6字节)而不是 MiB(2^20字节)单位,请使用 --block-size=MB。
如果您不想让文件大小带有 M 后缀,请使用 --block-size=1M 等类似选项。感谢 Stéphane Chazelas 提出此建议。
在 man ls 中查找 SIZE,可以了解更多信息。它还允许使用其他单位,看起来(我没有尝试过)也支持任意块大小(因此您可以看到文件大小以 412 字节块的数量表示)。
请注意,--block-size 参数是 GNU 对 Open Group 的 ls 的扩展,因此如果您没有 GNU 用户空间(大多数 Linux 安装都有),则可能无法使用此选项。GNU coreutils 8.5 中的 ls 支持上述 --block-size。
-l
和--block-size
标志。ls -l --block-size=M
工作目录下所有文件和目录的大小(以GB为单位)
ls -l --block-size=G
特定文件或目录的大小
ls -l --block-size=M my_file.txt
ls -l --block-size=M my_dir/
ls --help
-l
use a long listing format
--block-size=SIZE
: scale sizes bySIZE
before printing them; e.g.,'--block-size=M'
prints sizes in units of 1,048,576 bytes; seeSIZE
format below
SIZE
is an integer and optional unit (example: 10M is 10*1024*1024). Units are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).
如果您正在脚本中使用它,请使用stat
。
$ date | tee /tmp/foo
Wed Mar 13 05:36:31 UTC 2019
$ stat -c %s /tmp/foo
29
$ ls -l /tmp/foo
-rw-r--r-- 1 bruno wheel 29 Mar 13 05:36 /tmp/foo
这将给您以字节为单位的大小。有关更多输出格式选项,请参见man stat
。
$ date | tee /tmp/foo
Wed Mar 13 00:54:16 EDT 2019
$ stat -f %z /tmp/foo
29
$ ls -l /tmp/foo
-rw-r--r-- 1 bruno wheel 29 Mar 13 00:54 /tmp/foo
ls
命令无法显示目录中所有内容的总大小。 - Maxim Egorushkindu
命令显示文件的磁盘使用情况,这可能比文件的实际大小要大。您可以使用du -d
命令获取实际大小,就像ls
命令一样。更多信息请参考:https://unix.stackexchange.com/a/106278/155224 - Lucas Alonso