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


当前回答

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

安装

$ sudo apt-get install ncdu

分析

$ ncdu <directory>

其他回答

文件使用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

你可以使用:

ls lh

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

您所需要的只是-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——帮助 -l使用长列表格式 ——block-size=SIZE:在打印之前按SIZE缩放大小;例如, '——block-size=M'以。为单位打印大小 1048576个字节;参见下面的SIZE格式 “SIZE”为可选单位,取值为整型。例如:10M为10*1024*1024。 单位为K, M, G, T, P, E, Z, Y(1024的幂)或KB, MB,… (1000的幂)。


Du -sh [file_name]

可以完美地获取特定文件的大小。

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