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 occupy in a certain 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. Just pass the options and the path to the folder you want to work with:

$ du options /path/to/folder

Utility options:

-a, --all - output size for all files, not just directories, by default it is output only for folders;
-B, --block-size - specify size output units, available: K,M,G,T,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 nesting depth of directories;
-h, --human-readable - output size in human-readable units;
--inodes - output information about inode usage;
-L, --dereference - follow all symbolic links;
-l, --count-links - consider file size multiple times for hard links;
-P, --no-dereference - do not follow symbolic links, this is the default behavior;
-S, --separate-dirs - do not include subfolder size in folder size;
--si - output the size of files and folders in the c system, 1000 instead of 1024 is used;
-s, --summarize - output only the total size;
-t, --threshold - disregard files and folders with size smaller than specified;
--time - display the time of the last modification for a file or folder, instead of modification time you can display the following labels: atime, access, use, ctime;
-X, --exclude - exclude files from counting;
-x, --one-file-system - skip mounted file systems;
--version - display the version of the utility.

To view all options execute:

How to use the command?

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

$ man du

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

$ du -h /var

You can also specify the block size. Then the accuracy will be a bit lower, because the minimum unit 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 output the size of not only folders, but also the files that are located there use the -a option:

$ du -ha /var

To display only the total size of all files and folders, use the -s option:

$ du -hs /var

When you need to output the size of folders without subfolders attached to them, use the -m option:

$ du -hS /var

There is also an option to output a line with the total size of the whole folder. But you should use this option with the -S option:

$ du -hSc /va

To exclude everything in the log:

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

To sort values in a convenient format:

$ du -h /var | sort -h