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

Adv*_*ait 261 linux file

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

mk.*_*k.. 432

这很简单.使用ls命令用于目录的文件和du命令.

检查文件大小

ls -l filename /* Size of the file*/
ls -l *        /* Size of All the files in the current directory */
ls -al *       /* Size of All the files including hidden files in the current directory */
ls -al dir/    /* Size of All the files including hidden files in the 'dir' directory */
Run Code Online (Sandbox Code Playgroud)

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*/
Run Code Online (Sandbox Code Playgroud)

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

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

  • `ls`不会显示目录中所有内容的总大小. (7认同)
  • @FranciscoCorralesMorales ls -lh将显示kb MB GB等的大小 (6认同)
  • 请注意,`du`命令显示文件的磁盘使用情况,可能大于文件的实际大小.您可以使用`du -d`来获得实际大小,就像`ls`一样.更多信息:https://unix.stackexchange.com/a/106278/155224 (3认同)
  • @MaximYegorushkin谢谢.纠正. (2认同)
  • 我怎样才能看到以字节为单位的大小(Mega,Giga,...)? (2认同)

Max*_*kin 134

du命令.

目录大小:

$ du -sh .bashrc /tmp
Run Code Online (Sandbox Code Playgroud)

文件大小:

$ du -sh .bashrc /tmp
Run Code Online (Sandbox Code Playgroud)

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

  • @FranciscoCorralesMorales` -h`标志应该按照您的要求执行:_print尺寸的人类可读格式(例如,1K 234M 2G)_ (8认同)
  • 我怎样才能看到以字节为单位的大小(千,兆,千兆,...)? (4认同)
  • 我认为这是不对的。“ du”的作用是_汇总一组FILEs_的磁盘使用情况,也就是说,如果文件很小(即2140字节),则“ du”的输出(在我的情况下)为4KB,因为这就是集群的大小 (3认同)
  • 我喜欢`du -hs *`来查看当前目录中所有文件和目录的大小。 (3认同)

Tho*_*hor 66

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

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

  • 小费是要用的; 'du -sh*'列出所有目录大小.:) (6认同)
  • “ls -l -sh”是我一直在寻找的 (2认同)

小智 26

您可以使用:

ls -lh

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


Rav*_*dra 18

文件大小(MB)

ls -l --b=M  filename | cut -d " " -f5
Run Code Online (Sandbox Code Playgroud)

文件大小(GB)

ls -l --b=G  filename | cut -d " " -f5
Run Code Online (Sandbox Code Playgroud)


avt*_*ton 15

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

安装

Ubuntu的:

$ sudo apt-get install ncdu
Run Code Online (Sandbox Code Playgroud)

用法

只需输入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
Run Code Online (Sandbox Code Playgroud)

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


Adi*_*Adi 13

ls -l -block-size = M将为您提供长格式列表(实际查看文件大小所需)和圆形文件大小,最接近最接近的MiB.

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

如果您不希望M后缀附加到文件大小,则可以使用类似--block-size = 1M的内容.感谢StéphaneChazelas的建议.

这在ls的手册页中有描述; 男人ls并搜索SIZE.它允许MB/MiB以外的单位,并且从它的外观(我没有尝试)任意块大小(所以你可以看到文件大小为412字节块的数量,如果你想至).

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


Gio*_*ous 10

所有你需要的是-l--block-size标志

工作目录下所有文件和目录的大小(以 MB 为单位)

ls -l --block-size=M
Run Code Online (Sandbox Code Playgroud)

工作目录下所有文件和目录的大小(以GB为单位)

ls -l --block-size=G
Run Code Online (Sandbox Code Playgroud)

特定文件或目录的大小

ls -l --block-size=M my_file.txt
ls -l --block-size=M my_dir/
Run Code Online (Sandbox Code Playgroud)
ls --help
Run Code Online (Sandbox Code Playgroud)

-l 使用长列表格式

--block-size=SIZESIZE在打印之前缩放尺寸;例如, '--block-size=M'以 1,048,576 字节为单位打印大小;见SIZE下面的格式

SIZE是一个整数和可选单位(例如:10M 是 10*1024*1024)。单位是 K、M、G、T、P、E、Z、Y(1024 的幂)或 KB、MB、...(1000 的幂)。



sum*_*dhe 9

您可以使用ncdu磁盘使用分析器。它在 ncurses 界面中显示文件和目录的大小。您可以导航到每个目录并从同一界面查看文件大小。

安装

$ sudo apt-get install ncdu
Run Code Online (Sandbox Code Playgroud)

来分析

$ ncdu <directory>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Bru*_*sky 8

如果在脚本中使用它,请使用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
Run Code Online (Sandbox Code Playgroud)

这将为您提供字节大小。请参阅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
Run Code Online (Sandbox Code Playgroud)


Fok*_*est 7

我一直这样做:

$ du -sh backup-lr-May-02-2017-1493723588.tar.gz
Run Code Online (Sandbox Code Playgroud)

注意:

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


小智 7

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

ls -lrtsh


Cla*_*nas 7

转到所选目录并执行:

$ du -d 1 -h
Run Code Online (Sandbox Code Playgroud)

哪里:

-d 1 is the depth of the directories

-h is the human-readable option
Run Code Online (Sandbox Code Playgroud)

您会看到这样的内容:

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 .
Run Code Online (Sandbox Code Playgroud)


dar*_*der 7

我更喜欢这个命令ll -sha


mke*_*bri 6

使用带有 -h 参数的ls命令: [root@hots19 etc]# ls -lh
h :为了人类可读。

Exemple: 
    [root@CIEYY1Z3 etc]# ls -lh
    total 1.4M
    -rw-r--r--.  1 root   root      44M Sep 15  2015 adjtime
    -rw-r--r--.  1 root   root     1.5K Jun  7  2013 aliases
    -rw-r--r--   1 root   root      12K Nov 25  2015 aliases.db
    drwxr-xr-x.  2 root   root     4.0K Jan 11  2018 alternatives
    -rw-------.  1 root   root      541 Jul  8  2014 anacrontab
    -rw-r--r--.  1 root   root      55M Sep 16  2014 asound.conf
    -rw-r--r--.  1 root   root       1G Oct  6  2014 at.deny
Run Code Online (Sandbox Code Playgroud)


Eys*_*ika 6

du -sh [文件名]

非常适合获取特定文件的大小。


小智 5

要获取目录的总大小或文件使用的总大小,

du -csh <directory or filename*> | grep total
Run Code Online (Sandbox Code Playgroud)