如何从命令行获取Linux中目录的大小?

本文介绍了如何在Linux命令行中使用`du`和`sort`命令获取单个或多个目录的大小,并按大小进行排序。通过添加选项 `-s` 和 `-h` 可以得到人类可读的目录大小,使用通配符 `*` 或指定目录名称可以列出多个目录。同时,使用`sort -h -r`可以按降序排列目录大小,而移除 `-r` 则按升序排列。
摘要由CSDN通过智能技术生成

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 Directory
Find Size Of Directory
查找目录大小

查找多个目录的大小(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
Find Size Of Multiple Directories
查找多个目录的大小

通过指定目录名称查找多个目录的大小(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根据目录的大小列出了多个目录。 但是,如果我们需要专门提供目录名称,则可以通过使用空格分隔它们来添加名称。 在此示例中,我们将列出名为AndroidDockerfileeclipse目录。

$ du -sh Android Dockerfile eclipse
Find Size Of Multiple Directories By Specifying Directory Names
Find Size Of Multiple Directories By Specifying Directory Names
通过指定目录名称查找多个目录的大小

根据大小对多个目录进行排序(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
Sort Multiple Directories According To Their Size
根据大小对多个目录进行排序

根据大小对多个目录进行排序(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
Sort Multiple Directories According To Their Size
Sort Multiple Directories According To Their Size
根据大小对多个目录进行排序
LEARN MORE  Linux Bash Pipe Command Usage with Examples For Redirection
了解更多Linux Bash Pipe命令用法以及重定向示例

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值