检查目录和文件大小的有用命令

2025-06-10

检查目录和文件大小的有用命令

通常,需要检查项目中目录或文件的大小,或者评估目录在实时/开发服务器或本地机器上占用的空间。

这里列出了一些有用的命令,您可以利用这些命令来确保您的机器的可用磁盘空间正常,并且node_modules您的项目的目录大小没有变得太过庞大:D

我们将使用du 命令,其解释如下:

du (disc usage) command estimates file_path space usage

The options -sh are (from man du):

  -s, --summarize
         display only a total for each argument

  -h, --human-readable
         print sizes in human readable format (e.g., 1K 234M 2G)

要检查多个目录并查看总数,请使用 du -sch:

  -c, --total
         produce a grand total

那么,让我们看看基本命令:

以可读格式显示目录的大小:

$ du -sh directory/

显示所有目录及其大小的列表,按大小排序:

$ du -sh * | sort -h

我的示例 Flutter 项目中的示例:

paul@paul-Inspiron-N5110:~/projects/flutter_test$ du -sh * | sort -h
4,0K    android.iml
4,0K    flutter_test_android.iml
4,0K    flutter_test.iml
4,0K    pubspec.yaml
4,0K    README.md
8,0K    test
12K lib
200K    android
212K    ios

此外,如果您想包含隐藏文件和目录,您可以运行:

$ du -sch .[^.]* * |sort -h

奖金:

输出系统分区的总大小和可用大小

$ df -h

输出系统中的inode使用情况:

$ df -i

如果您有更多有用的命令可以分享,请在下面的评论中留下它们!

鏂囩珷鏉ユ簮锛�https://dev.to/pavlosisaris/useful-commands-to-check-directory-size-1gkj
PREV
我们应该知道的基本 Linux/Unix 命令
NEXT
PHP 8 来了!朝着正确方向迈出的一步?新功能介绍