Methods of payment Abuse

Du command in Linux

17.11.2021, 14:04

In some cases, you may need to see how much space files take up in a particular folder and find the largest files to delete. There are several tools to solve these tasks, but the simplest of them is the du utility. It allows you to display the size of all files in a certain folder in bytes or in a more convenient format. Let's see what the du Linux command is and how to use it to solve your work tasks.

Command options

The syntax is simple. It is enough to pass options and path to the folder you want to work with:

$ du options /path/to/folder

Utility Options:

  • -a, --all output the size for all files, not just directories, by default it is output only for folders;
  • -B, --block-size specify the size output units, available: K,M,G,T,P,E,Z,Y for 1024 and KB, MB and so on for 1000;
  • -c, --total output the total size of all folders at the end;
  • -d, --max-depth maximum directory nesting depth;
  • -h, --human-readable  output the size in human-friendly units;
  • --inodes - output information about inode usage;
  • -L, --dereference - to follow all symbolic links;
  • -l, --count-links - consider the file size multiple times for hard links;
  • -P, --no-dereference - not to follow symbolic links, this is the default behavior;
  • -S, --separate-dirs - Do not include the size of subfolders in the folder size;
  • --si - выводить размер файлов и папок в системе си, используется 1000 вместо 1024;
  • -s, --summarize - output only the total size;
  • -t, --threshold - do not consider files and folders with a size smaller than the specified size;
  • --time - display the last modification time for a file or folder, instead of modification time you can display the following labels: atime, access, use, ctime;
  • -X, --exclude - to exclude files from the count;
  • -x, --one-file-system - skip mounted file systems;
  • --version - display the version of the utility.

To view all options, follow the steps below:

How do I use the command?

To simply display a list of folders in a particular directory and the space they occupy, e.g. in /var, run:

$ man d

If you want the size to be displayed in a more readable form, use the option-h:

$ du -h /var

You can also specify the block size. Then the accuracy will be a bit lower because the minimum unit of measure is one block. For example, to output the size of folders in megabytes with a 1024 kilobyte block, use the -B option with the M parameter:

$ du -BM /var

You can display not only the size of folders, but also the size of files that are located there, use the option -a:

$ du -BM /var

To display only the total size of all files and folders you need to apply the option -s:

$ du -BM /var

When it is necessary to display the size of folders without subfolders, use the option -m:

$ du -BM /var

There is also an option to display a line with the total size of the entire folder. But it is advisable to use this feature with the option -S:

$ du -hSc /va

To eliminate everything in the log:

$ du -hac --exclude="*.log"

To sort values in a convenient format:

$ du -hac --exclude="*.log"