du 命令

...

在 Linux 中,你可以使用 du(Disk Usage)命令来查看当前目录下所有内容占用的磁盘大小。以下是一些常用的命令示例:

  1. 查看当前目录下所有文件和文件夹的大小
    du -h --max-depth=1

• -h:以人类可读的方式显示大小(例如 KB、MB、GB)。
• --max-depth=1:只显示当前目录下的文件和文件夹的大小,而不递归深入子目录。
2. 查看当前目录的总大小
du -sh

• -s:只显示总计大小。
• -h:以人类可读的方式显示大小。
3. 查看当前目录下最大的文件或文件夹
du -h --max-depth=1 | sort -hr

• sort -hr:按大小从大到小排序(-h 表示支持人类可读格式,-r 表示逆序排列)。
4. 查看某个特定文件或文件夹的大小
du -h /path/to/file_or_folder

通过这些命令,你可以轻松了解当前目录下文件和文件夹的磁盘占用情况。希望对你有帮助!

du --help
Usage: du [OPTION]... [FILE]...
  or:  du [OPTION]... --files0-from=F
Summarize disk usage of the set of FILEs, recursively for directories.

Mandatory arguments to long options are mandatory for short options too.
  -0, --null            end each output line with NUL, not newline
  -a, --all             write counts for all files, not just directories
      --apparent-size   print apparent sizes, rather than disk usage; although
                          the apparent size is usually smaller, it may be
                          larger due to holes in ('sparse') files, internal
                          fragmentation, indirect blocks, and the like
  -B, --block-size=SIZE  scale sizes by SIZE before printing them; e.g.,
                           '-BM' prints sizes in units of 1,048,576 bytes;
                           see SIZE format below
  -b, --bytes           equivalent to '--apparent-size --block-size=1'
  -c, --total           produce a grand total
  -D, --dereference-args  dereference only symlinks that are listed on the
                          command line
  -d, --max-depth=N     print the total for a directory (or file, with --all)
                          only if it is N or fewer levels below the command
                          line argument;  --max-depth=0 is the same as
                          --summarize
      --files0-from=F   summarize disk usage of the
                          NUL-terminated file names specified in file F;
                          if F is -, then read names from standard input
  -H                    equivalent to --dereference-args (-D)
  -h, --human-readable  print sizes in human readable format (e.g., 1K 234M 2G)
      --inodes          list inode usage information instead of block usage
  -k                    like --block-size=1K
  -L, --dereference     dereference all symbolic links
  -l, --count-links     count sizes many times if hard linked
  -m                    like --block-size=1M
  -P, --no-dereference  don't follow any symbolic links (this is the default)
  -S, --separate-dirs   for directories do not include size of subdirectories
      --si              like -h, but use powers of 1000 not 1024
  -s, --summarize       display only a total for each argument
  -t, --threshold=SIZE  exclude entries smaller than SIZE if positive,
                          or entries greater than SIZE if negative
      --time            show time of the last modification of any file in the
                          directory, or any of its subdirectories
      --time=WORD       show time as WORD instead of modification time:
                          atime, access, use, ctime or status
      --time-style=STYLE  show times using STYLE, which can be:
                            full-iso, long-iso, iso, or +FORMAT;
                            FORMAT is interpreted like in 'date'
  -X, --exclude-from=FILE  exclude files that match any pattern in FILE
      --exclude=PATTERN    exclude files that match PATTERN
  -x, --one-file-system    skip directories on different file systems
      --help     display this help and exit
      --version  output version information and exit

Display values are in units of the first available SIZE from --block-size,
and the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.
Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).

The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/du>
or available locally via: info '(coreutils) du invocation'

常见用法

显示某文件大小

# 显示某文件大小
du -sh xxxfile
20M xxxfile

排除特定目录

在使用 du -sh 命令时,如果想要排除某个特定的目录,可以结合 --exclude 选项来实现。以下是具体的用法:

注意:macos 下 du 命令不支持改参数

du -sh --exclude=DIR --exclude=folder2 --exclude=folder3
# or
du -sh --exclude=".*"  # 排除所有隐藏文件和隐藏目录
  • DIR 是你想要排除的目录名称。
  • du -sh 将会计算当前目录的总大小,但会忽略掉 --exclude 参数中指定的目录。
  • 如果需要排除多个目录,可以多次使用 --exclude:

macos 显示某代码库大小

macOS 的 du 命令功能相对有限,-I mask 并不能像 Linux 的 --exclude 那样灵活。
-I mask 选项允许你指定一个通配符模式(mask),以忽略符合该模式的文件或目录。例如:

du -sh .
# or
du -sh .git
# or 排除 .git 目录
du -sh -I ".git"
posted @ 2020-11-18 16:14  taadis  阅读(221)  评论(0)    收藏  举报
扫码关注

扫码关注我