本文為joshua317原創文章,轉載請注明:轉載自joshua317博客 https://www.joshua317.com/article/160
一、簡介
Linux下的du (英文全拼:disk usage)命令用於顯示目錄或文件所占磁盤空間的大小,但是與df命令不同的是du主要查看的目錄或文件所占用的磁盤空間,而df是主要查看磁盤的空間。
二、格式說明
du [OPTION]... [FILE]...
or: du [OPTION]... --files0-from=F
du [選項]... [文件]...
-0, --null end each output line with 0 byte rather than 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).
SIZE is an integer and optional unit (example: 10M is 10*1024*1024). Units
are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).
三、選項說明
-a -all 顯示目錄中個別文件的大小
-B <塊大小> 指定顯示時的塊大小
-b -bytes 顯示目錄或文件大小時,以byte為單位,相當於'--apparent-size --block-size=1'
-c --total 除了顯示個別目錄或文件的大小外,同時也顯示所有目錄或文件的總和
-D --dereference-args 顯示指定符號連接的源文件大小
-d --max-depth=<目錄層數>,超過指定層數的目錄后,予以忽略
-h --human-readable 以K,M,G為單位,提高信息的可讀性
-H --si 與-h參數相同,但是K,M,G是以1000為換算單位
-k --kilobytes 以1024 bytes為單位,即是以KB為單位顯示文件大小
-l --count-links 重復計算硬件連接的文件
-L<符號連接> --dereference<符號連接> 顯示選項中所指定符號連接的源文件大小
-m --megabytes 以1MB為單位,即是以MB為單位顯示文件大小
-s --summarize 僅顯示總計
-S --separate-dirs 顯示個別目錄的大小時,並不含其子目錄的大小
-t --threshold=SIZE 排除小於SIZE的文件
-x --one-file-xystem 以一開始處理時的文件系統為准,若遇上其它不同的文件系統目錄則略過
-X<文件> --exclude-from=<文件> 在<文件>指定目錄或文件
--exclude=<目錄或文件> 略過指定的目錄或文件
--max-depth=<目錄層數> 超過指定層數的目錄后,予以忽略
--time 顯示目錄中文件或其子目錄的最后修改時間
--time=WORD 指定顯示哪種時間,atime, access, use, ctime or status
--time-style=STYLE 使用STYLE顯示時,如:full-iso, long-iso, iso,或+FORMAT;格式的解釋類似於'date'
--help 顯示幫助
--version 顯示版本信息
四、命令功能
顯示目錄或文件所占磁盤空間的大小
五、常見用法
5.1 顯示當前目錄或者文件所占磁盤空間的大小
# du
4 ./test3/test2/test5
4 ./test3/test2/tmp
4 ./test3/test2/hello.java
4 ./test3/test2/test4
24 ./test3/test2
28 ./test3
4 ./test2/test5
4 ./test2/hello.java
4 ./test2/test4
16 ./test2
60 .
5.2 以易讀方式顯示當前目錄或者文件所占磁盤空間的大小
# du -h
4.0K ./test3/test2/test5
4.0K ./test3/test2/tmp
4.0K ./test3/test2/hello.java
4.0K ./test3/test2/test4
24K ./test3/test2
28K ./test3
4.0K ./test2/test5
4.0K ./test2/hello.java
4.0K ./test2/test4
16K ./test2
60K .
5.3 顯示所有目錄或者文件的大小
0 ./test5
4.0K ./tree.txt
0 ./test1.txt
0 ./link_test.txt
0 ./hard_link_test.txt
0 ./test3/test2.txt
4.0K ./test3/test2/test2.txt
4.0K ./test3/test2/test5
4.0K ./test3/test2/tmp
4.0K ./test3/test2/hello.java
4.0K ./test3/test2/test4
24K ./test3/test2
28K ./test3
4.0K ./test.txt
0 ./test1_link.txt
0 ./test2/test2.txt
4.0K ./test2/test5
4.0K ./test2/hello.java
4.0K ./test2/test4
16K ./test2
4.0K ./ip.txt
60K .
5.4 顯示當前目錄下及子目錄所使用的空間
# du -h --max-depth=1
28K ./test3
16K ./test2
60K .
注意:--max-depth 主要顯示指定層數的目錄
5.5 顯示當前目錄所占用的磁盤空間
# du -sh
60K .
5.6 顯示指定文件所占的空間
# du -h test.txt
4.0K test.txt
5.7 顯示多個文件所占空間
# du -h ip.txt test.txt
4.0K ip.txt
4.0K test.txt
5.8 顯示幾個文件或目錄各自占用磁盤空間的大小,還統計它們的總和
# du -hc ip.txt test.txt
4.0K ip.txt
4.0K test.txt
8.0K total
本文為joshua317原創文章,轉載請注明:轉載自joshua317博客 https://www.joshua317.com/article/160