The Linux command du

Source: Internet
Author: User

The du command is used to view the disk space occupied or allocated by the specified file, in blocks of disk. If the parameter is a directory, it displays * The total space occupied by all the files in that directory *, and recursively lists the total footprint of all levels of subdirectories, but does not have information for non-catalog files . If no file name is specified, the current directory is assumed to be the default:

m@sys:~/program/C_codes/tools$ du16  ./calculators28  .

Note that the statistical value given here refers to the total amount of all file sums in that directory, not the individual directory file itself, as stated in the previous article, the LS directory has an option-s can also show the size of the disk space occupied by the file, let us compare:

[Email protected]:~/program/c_codes/tools$ ls-lasTotal dosage -4Drwxrwxr- x 3Mm4096  4Month -  One: - .4Drwxrwxr- x 5Mm4096  5Month A  +: - ..4Drwxrwxr- x 2Mm4096  5Month4 xx: -Calculators4 -RW-RW- R-- 1Mm422  3Month A  +: -Formtrans.C4 -RW-RW- R-- 1Mm to  4Month7  -: -Readme.Md

The contrast is obvious, ls-s only the directory as a normal file to calculate the space occupied by itself, and Du is to calculate all the files in the directory occupy the sum of space (including the directory file itself). Here you need a bit of Linux file and disk knowledge, you can refer to this article.
As can be seen from the results of LS above, Linux allocates a disk space of at least 4 blocks per file, regardless of its actual size, and the directory is basically 4 blocks.

Here are the various options:

    • -A: When the parameter is a directory, it also lists the size of the various files that the directory contains, not just the levels of subdirectories.

      • -C: The total occupied space of the explicit statistics directory, that is, there will be a single row to display the ' total ' or ' aggregate usage '. In fact, this option is optional, because the du display of the specified directory occupies space is actually the total amount of this directory:
 m@sys:~/program/C_codes/tools/calculators$ du -ca4   ./calculator.l4   ./Makefile4   ./calculator.y16  .16  总用量

can see, '. ' The table of contents (current directory) is the same as the result of total usage.

    • -B: Specifies the display unit instead of the default block. In general, the size of the block is 4096 bytes, which is specified when the disk (MKFS command) is formatted, and can be viewed like this:
m@sys:~$ "Block size"Blocksize:               4096

What are the parameters that can be followed by-B? Two sets of standards: industry standards and computer standards.

    • In computer standards, the units are:1, K, M, G .... the multiples of the neighboring units here are 1024, the number ' 1 ' refers to a byte, 1K = 1024 bytes, 1M = 1024K ...;
    • In industry standards, the units are:1, KB, MB, GB ... The multiple of adjacent units is 1000,1k = 1000 bytes, 1M = 1000K ...
      Units can be preceded by a number
 m@sys:~/program/C_codes/tools$ du -B116384   ./calculators28672   .m@sys:~/program/C_codes/tools$ du -B k16K ./calculators28K .
    • --apparent-size: Displays the actual size of the file, not the size of the allocated space:
m@sys:~/program/C_codes/tools$ du --apparent-size6   ./calculators10  .m@sys:~/program/C_codes/tools$ du16  ./calculators28  .

From the results to see, add this parameter difference is still very obvious; By default, in block, you can add the-B 1 option in bytes to display the same size as ls-l:

m@sys:~$ du --apparent-size -B1 formtrans.c  422 formtrans.cm@sys:~$ 1422  31221:24 formtrans.c

However, this trick is not used for catalog files, because, as stated above, Du always calculates the sum of all the files it contains, rather than itself, for the directory:

M@sys: ~/program/c_codes/tools/calculators$ Ls-al Total Dosage -Drwxrwxr-x2Mm4096  5Month4 xx: -. drwxrwxr-x3Mm4096  4Month -  One: -.. -rw-rw-r--1Mm340  4Month -  -:Tencalculator.l-rw-rw-r--1Mm673  5Month4 xx: -calculator.y-rw-rw-r--1Mm367  4Month -  -: - MakefileM@sys: ~/program/c_codes/tools/calculators$ Du--apparent-size-B 1-A.340./calculator.l367./Makefile673./calculator.y5476.

We can calculate the result of the following: 5476-673-367-340 = 4096, which is exactly the actual size of a directory file itself, as the result of ls-l.

    • -B: equivalent to –apparent-size–block-size=1
 m@sys:~/program/C_codes/tools/calculators$ du -a --apparent-size -B1340 ./calculator.l367 ./Makefile673 ./calculator.y5476    .m@sys:~/program/C_codes/tools/calculators$ du -ab340 ./calculator.l367 ./Makefile673 ./calculator.y5476    .
    • -K: equivalent to-B 1k
 m@sys:~/program/C_codes/tools/calculators$ du -a -B1k4   ./calculator.l4   ./Makefile4   ./calculator.y16  .m@sys:~/program/C_codes/tools/calculators$ du -a -k4   ./calculator.l4   ./Makefile4   ./calculator.y16  .
    • -M: similar to-K. In fact, options such as-K and-M appear in many commands.

    • -S: Subtracts the size of all subdirectories from the total result:

 m@sys:~/program/C_codes/tools$ du --apparent-size -B1 -S .5476    ./calculators4549    .m@sys:~/program/C_codes/tools$ du --apparent-size -B1  .5476    ./calculators10025   .
    • -h:print sizes in human readable format, there's really nothing to say.
    • -S: Displays only the total amount of the directory, not all levels of subdirectories.
    • -D: Specifies the number of levels of subdirectories to display, such as:-D 0 is the directory itself, equivalent to-s;-d 1 is to display a sub-directory at the same time;-D 2 Displays the Sun directory ... And so on

Complete the full text.

The Linux command du

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.