如何用Linux命令获取特定文件夹中文件和目录的大小?

563

如何在Linux中查看文件和目录的大小?使用df -m命令,可以显示顶层目录下所有目录的大小,但是对于目录和文件内部的内容,如何检查它们的大小呢?

22个回答

840

使用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)将以人类可读的格式显示大小(kbmbgb等)。

更多信息请参见man lsman du


8
ls 命令无法显示目录中所有内容的总大小。 - Maxim Egorushkin
4
@MaximYegorushkin 谢谢。已更正。 - Sandeep
6
如何查看文件大小(以字节,兆字节,吉字节等方式表示)? - Francisco Corrales Morales
16
ls -lh命令会显示文件或目录的大小,以kb、MB或GB等单位表示。 - Sandeep
7
请注意,du 命令显示文件的磁盘使用情况,这可能比文件的实际大小要大。您可以使用 du -d 命令获取实际大小,就像 ls 命令一样。更多信息请参考:https://unix.stackexchange.com/a/106278/155224 - Lucas Alonso
显示剩余9条评论

222

du 命令。

整个目录和/或文件的大小(-s),多个参数的总计(-c),以人类友好的方式显示(-h):

$ du -sch ~/.bashrc /tmp $XDG_RUNTIME_DIR 
8.0K    /home/max/.bashrc
358M    /tmp
80K /run/user/1000
359M    total

我把 "du -sch" 记成了德语单词 "dusch"(英文意思是 "shower")。

--apparent-size命令行开关使其测量表面大小(即ls显示的大小),而不是实际磁盘使用量。


4
我怎样才能看到以字节为单位的文件大小(千字节,兆字节,吉字节等)? - Francisco Corrales Morales
14
-h 标志应该能够实现你所要求的功能:以人类可读的格式打印大小(例如,1K 234M 2G)。 - Maxim Egorushkin
3
我认为这不正确。"du" 的作用是“汇总一组文件的磁盘使用情况”,也就是说,如果一个文件非常小(如 2140 字节),则 "du" 的输出(在我的情况下)为 4KB,因为那是簇的大小。 - mfloris
@mfloris 那就使用 --apparent-size - Maxim Egorushkin
11
我喜欢使用 du -hs * 命令来查看当前目录中所有文件和文件夹的大小。 - Jordan Stewart

155

使用ls -s列出文件大小,或者如果您更喜欢,可以使用ls -sh以人类可读的方式显示文件大小。

对于目录,请使用du,同样,使用du -h以人类可读的方式显示文件大小。


16
一个提示是使用命令 'du -sh *' 来列出所有目录的大小。 :) - Entalpi
5
“ls -l -sh”是我一直在寻找的。 - Am1rFT

62

还有一个很好的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 退出


57
你可以使用以下命令来查看目录的表面空间和文件的真正空间大小,以及每个文件的详细信息,包括文件名、大小和创建日期:

ls -lh


“ls -l” 命令会显示包括元数据在内的文件大小吗?因为我尝试过其中一个文件,似乎要大 4kb 左右。 - Jun

34

前往选择的目录并执行:

$ 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 .

32

文件大小(以MB为单位)

ls -l --b=M  filename | cut -d " " -f5

文件大小(以GB为单位)

ls -l --b=G  filename | cut -d " " -f5

如何将这两个字段作为输出? - surbhiGoel
1
我建议在使用cut之前,用sed将重复的空格替换为一个空格:ls -l filename | sed -E -e 's/ +/ /g' | cut -d " " -f5 - Israel Shainert

19

使用命令 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。


19
你只需要使用-l--block-size标志。
工作目录下所有文件和目录的大小(以MB为单位)。
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 by SIZE before printing them; e.g., '--block-size=M' prints sizes in units of 1,048,576 bytes; see SIZE 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).



13

如果您正在脚本中使用它,请使用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

OSX/BSD的相应命令是:

$ 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接