如何在linux中查看文件和目录的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11720079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:58:03  来源:igfitidea点击:

How can I see the size of files and directories in linux?

linuxfile

提问by Advait

How can I see the size of files and directories in Linux? If use df -m, then it shows the size of all the directory at the top level, but, for the directories and files inside the directory, how do I check the size?

如何在 Linux 中查看文件和目录的大小?如果使用df -m,则显示顶层所有目录的大小,但是,对于目录内的目录和文件,如何检查大小?

采纳答案by mk..

Use lscommand for files and ducommand for directories.

ls对文件使用命令,du对目录使用命令。

Checking File Sizes

检查文件大小

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

lscommand will not list the actual size of directories(why?). Therefore, we use dufor this purpose.

ls命令不会列出目录的实际大小(为什么?)。因此,我们du用于此目的。

Checking Directory sizes

检查目录大小

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

Including -hoption in any of the above commands (for Ex: ls -lh *or du -sh) will give you size in human readable format (kb, mb,gb, ...)

包括-h选择上述任何命令(用于例如:ls -lh *du -sh)会给你在人类可读的格式大小(kbmbgb,...)

For more information see man lsand man du

有关更多信息,请参阅man lsman du

回答by Maxim Egorushkin

There is ducommand.

du命令。

Size of a directory and/or file:

目录和/或文件的大小:

$ du -sh .bashrc /tmp


--apparent-sizecommand line switch makes it measure apparent sizes (what lsshows) rather than actual disk usage.

--apparent-size命令行开关使其测量表观大小(ls显示的内容)而不是实际磁盘使用情况。

回答by Thor

Use ls -sto list file size, or if you prefer ls -shfor human readable sizes.

使用ls -s到列表文件的大小,或者如果你喜欢ls -sh的人可读的大小。

For directories use du, and again, du -hfor human readable sizes.

对于目录du,请再次使用,du -h对于人类可读的大小。

回答by Adi

ls -l --block-size=M will give you a long format listing (needed to actually see the file size) and round file sizes up to the nearest MiB.

ls -l --block-size=M 将为您提供长格式列表(需要实际查看文件大小)并将文件大小四舍五入到最接近的 MiB。

If you want MB (10^6 bytes) rather than MiB (2^20 bytes) units, use --block-size=MB instead.

如果您想要 MB(10^6 字节)而不是 MiB(2^20 字节)单位,请改用 --block-size=MB。

If you don't want the M suffix attached to the file size, you can use something like --block-size=1M. Thanks Stéphane Chazelas for suggesting this.

如果您不想将 M 后缀附加到文件大小,则可以使用 --block-size=1M 之类的内容。感谢 Stéphane Chazelas 提出这个建议。

This is described in the man page for ls; man ls and search for SIZE. It allows for units other than MB/MiB as well, and from the looks of it (I didn't try that) arbitrary block sizes as well (so you could see the file size as number of 412-byte blocks, if you want to).

这在 ls 的手册页中有所描述;man ls 并搜索 SIZE。它也允许使用 MB/MiB 以外的单位,并且从它的外观来看(我没有尝试过)任意块大小(因此如果需要,您可以将文件大小视为 412 字节块的数量)到)。

Note that the --block-size parameter is a GNU extension on top of the Open Group's ls, so this may not work if you don't have a GNU userland (which most Linux installations do). The ls from GNU coreutils 8.5 does support --block-size as described above.

请注意,--block-size 参数是 Open Group ls 之上的 GNU 扩展,因此如果您没有 GNU 用户空间(大多数 Linux 安装都有),这可能不起作用。来自 GNU coreutils 8.5 的 ls 确实支持 --block-size 如上所述。

回答by avtomaton

There is also a great ncduutility - it can show directory size with detailed info about subfolders and files.

还有一个很棒的ncdu实用程序 - 它可以显示目录大小以及有关子文件夹和文件的详细信息。

Installation

安装

Ubuntu:

Ubuntu:

$ sudo apt-get install ncdu

Usage

用法

Just type ncdu [path]in the command line. After a few seconds for analyzing the path, you will see something like this:

只需ncdu [path]在命令行中输入即可。几秒钟后分析路径,您将看到如下内容:

$ 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

Delete the currently highlighted element with d, exit with CTRL+ c

用 删除当前突出显示的元素d,用CTRL+退出c

回答by Ravichandra

File Size in MB

文件大小 (MB)

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

File Size in GB

文件大小 (GB)

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

回答by Yenier Torres

You can use:

您可以使用:

ls -lh

ls -lh

Using this command you'll see the apparent space of the directory and true space of the files and in details the names of the files displayed, besides the size and creation date of each.

使用此命令,您将看到目录的表观空间和文件的真实空间,以及显示的文件名称的详细信息,以及每个文件的大小和创建日期。

回答by Prem S

you can use ls -sh in linux you can do sort also you need to go to dir where you want to check the size of files

您可以在 linux 中使用 ls -sh 您可以进行排序还需要转到要检查文件大小的目录

回答by Fokwa Best

I do the following all the time:

我一直在做以下事情:

$ du -sh backup-lr-May-02-2017-1493723588.tar.gz

NB:

注意:

-s, --summarize
            display only a total for each argument
-h, --human-readable
            print sizes in human readable format (e.g., 1K 234M 2G)

回答by Amit

You can use below command to get list of files in easily human readable format.

您可以使用以下命令以易于人类阅读的格式获取文件列表。

ls -lrtsh

ls -lrtsh