While working with Linux we may need to get the size of the specific directory. In GUI there are file managers like Dolphin, Gnome, Nautilus etc. But what if we need to get the size in command line. In this tutorial we will look how to get single or multiple directories sizes and sort them.
在使用Linux时,我们可能需要获取特定目录的大小。 在GUI中,有文件管理器,如Dolphin,Gnome,Nautilus等。但是,如果我们需要在命令行中获取大小,该怎么办。 在本教程中,我们将研究如何获取单个或多个目录大小并对其进行排序。
查找目录大小 (Find Size Of Directory)
We will start by simply listing the size of a directory with command du
. We will use -s
option for summary and -h
option for human readable format. We also need to specify the directory or folder name. In this example we will get size of directory named Android
.
我们将从简单地使用du
列出目录的大小开始。 我们将-s
选项用于摘要,将-h
选项用于人类可读的格式。 我们还需要指定目录或文件夹名称。 在此示例中,我们将获取名为Android
的目录的大小。
$ du -sh Android
$ du -sh Android

查找多个目录的大小(Find Size Of Multiple Directories)
We may need to find multiple directories sizes in given path. We just need to change the directory name with a glob which will list all directories and files sizes.
我们可能需要在给定路径中找到多个目录大小。 我们只需要用一个glob更改目录名,它将列出所有目录和文件的大小。
$ du -sh *

通过指定目录名称查找多个目录的大小(Find Size Of Multiple Directories By Specifying Directory Names)
In previous example we have listed multiple directories according to their sizes by using glob
. But if we need to specifically provide the directory names we can just add their names by separating them with a space. In this example we will list directories named Android
, Dockerfile
and eclipse
.
在前面的示例中,我们使用glob
根据目录的大小列出了多个目录。 但是,如果我们需要专门提供目录名称,则可以通过使用空格分隔它们来添加名称。 在此示例中,我们将列出名为Android
, Dockerfile
和eclipse
目录。
$ du -sh Android Dockerfile eclipse

根据大小对多个目录进行排序(Sort Multiple Directories According To Their Size)
While listing the directory sizes we may need to sort them according to their size in incremental mode. We can use sort
command with -h
option which is for human readable format for sizes and -r
option for reverse list from highest ro lowest.
在列出目录大小时,我们可能需要在增量模式下根据其大小对它们进行排序。 我们可以使用带有-h
选项的sort
命令,该选项用于人类可读格式的大小,而-r
选项用于从最高ro到最低的反向列表。
$ du -sh * | sort -hr

根据大小对多个目录进行排序(Sort Multiple Directories According To Their Size)
We can also list directories according to their size in a incremental manner by not using -r
option in du
command.
通过不使用du
命令中的-r
选项,我们还可以根据目录的大小以递增方式列出目录。
$ du -sh * | sort -h

翻译自: https://www.poftut.com/how-to-get-the-size-of-directory-in-linux-from-command-line/