Under linux use du to see how much disk space a file or directory takes up

  • 2020-05-13 04:21:58
  • OfStack

du-ah -- max-depth =1 this is what I want a to show all the files and folders in the directory (not including subdirectories), h to show all the files and folders in the directory (not including subdirectories), h to show all the files and folders in a human-readable way, and max-depth to show the depth of the directory.

The du command is used to see how much disk space a directory or file takes up. The commonly used combination of options is: du-sh

1. Functions of du: 'du' reports amount of disk used specified files and each subdirectory (of directory arguments). with no arguments, 'du' the disk space for the current directory

Obviously, unlike df, it is used to see how much disk space a file or directory takes up.

2. Common du options:

-h: displayed in a human readable manner

-a: shows how much disk space a directory takes up, as well as how much disk space its underlying directories and files take up

-s: shows how much disk space a directory occupies. Do not show how much disk space its subdirectories and files occupy

-c: displays the amount of disk space taken up by several directories or files, and counts their total

-- apparent-size: displays the size of a directory or file itself

-l: statistics on how much hard links take up disk space

-L: counts the amount of disk space used by the file to which the symbolic link points

1. du-h: I won't go into that.

2. du-a: use this option to display the size of the disk space occupied by directories and subdirectories and files.

As you can see, by default du only shows how much disk space the download directory takes up, but with the -a option, it shows not only the directory (the last line), but also how much disk space each file in the directory takes up.

3. du-s: with this option, du displays only the size of the disk space occupied by the directory, not the disk space occupied by the subdirectories and files.

By default, du does not show disk space taken up by files in a directory, but it does show disk space taken up by subdirectories. With the -s option, only the size of the disk space occupied by the xx directory is displayed.

4. du -- apparent-size: shows the size of the files or directories themselves, not the amount of disk space they take up. The size of a file or directory on disk is sometimes not exactly the same as its own size. This phenomenon is not unique to linux, nor is it unique to windows. Let's look at the explanation of the help documentation for this option: The apparent of a file file the number number reported wc regular or more generally For example, a the word zoo no no would, of newline would, of course have an apparent size of 3. depending on the type configuration the file system on the file resides

This paragraph shows the difference between the size of a file or directory itself and the amount of disk space it takes up. He gives an even more dramatic example below, which I won't write down here. We know: wc or ls -- block-size shows its size, while du shows how much disk space it takes up.

5. du-c: when you use this option, you not only display how much disk space several files or directories each take up, but you also count their total.

As shown in the figure, with the -c option, du not only shows how much disk space each of the two directories takes up, but also counts their total in the last line.

6. du-l: this option is mainly for hard links. When the statistics directory takes up a large amount of disk space, the -l option counts hard links as well. The help document explains: Count the size of all files,even if have appeared already(as a hard link).

We create a hard link in the download directory to a file in this directory. When not using the -l option, du finds a hard link to a file in this directory and automatically ignores the link file to avoid duplicate statistics. Using the -l option, du counts hard link files as well.

7. du-L: this option is mainly for symbolic links. It counts the amount of disk space taken up by the file to which the symbolic link points. The help document explains this: Dereference symbolic links(show the used by file instead of of link).

We create a symbolic link in the download directory to a file in this directory. As you can see in the image above, when using the -L option, du counts the size of the file to which the symbolic link points.

In addition, if we look at the results of du-lh, which is the same as du-h, we don't seem to have calculated the size of the symbolic link; Not really. In practice, symbolic links themselves don't seem to take up disk space, which is why this happens.

Finally, the du command does not count link files (either hard or symbolic) by default when the directory takes up disk space, so use the -l and -L options to specify this. In addition, the du command does not display disk space occupied by files in a directory by default, but it does display disk space occupied by subdirectories by default. Oddly.

du-sh: view the total capacity of the current directory. Instead of listing the capacity of each subitem separately

du-lh -- max-depth =1: view the disk capacity of level 1 subfiles and subdirectories in the current directory.


Related articles: