Sometimes it becomes necessary to see how much space the files in a certain folder take up and find the largest files in order to delete them. Of course, there are many tools to solve these problems, but the simplest of them is the du utility. It allows you to display the size of all files in a specific folder in bytes or in a more convenient format.
In today’s article, we will understand what the du Linux command is, as well as how to use it to solve your work tasks.
Syntax and options for the du command
The command syntax is very simple. You just need to pass it options and the path to the folder you want to work with:
$ du options /path/to/folder
And here are the utility options:
- -a, –all – display size for all files, not just for directories, by default the size is displayed only for folders;
- -B, –block-size – specify size output units, available: K,M,G,T,P,E,Z,Y for 1024 and KB, MB and so on for 1000;
- -c, –total – display the total size of all folders at the end;
- -d, –max-depth – maximum depth of directory nesting;
- -h, –human-readable – display size in human-friendly units;
- –inodes – display information about inode usage;
- -L, –dereference – follow all symbolic links;
- -l, –count-links – count file size multiple times for hard links;
- -P, –no-dereference – don’t follow symbolic links, this is the default behavior;
- -S, –separate-dirs – do not include the size of subfolders in the folder size;
- –si – display the size of files and folders in the si system, 1000 is used instead of 1024;
- -s, –summarize – display only the total size;
- -t, –threshold – ignore files and folders smaller than the specified size;
- –time – display the last modification time for a file or folder, instead of the 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.
Not all options are listed here. If you need more, see:
man du
du examples
To simply display a list of folders in a particular directory and the space they occupy, for example, in /var , run:
du /var
By default, the size is displayed in bytes. To display the size in a more readable way, use the -h option :
du -h /var
Or you can specify the block size. Then the accuracy will be a little lower, because the minimum unit of measure is one block. For example, to display the size of folders in megabytes with a block size of 1024 kilobytes, use the -B option with the M option :
du -BM /var
If you need to display 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
If you want to display the size of folders without their nested subfolders, use the -m option :
du -hS /var
You can also display a line with the total size of the entire folder. True, it makes sense to use this feature only with the -S option, because the total size of the folder in all other cases is already displayed like this:
du -hSc /var
If you need to exclude any files from the count, you should use the -exclude option. For example, let’s exclude all log files:
du -hac --exclude="*.log"
To make the data more visual, it is desirable to sort it. du linux does not have built-in support for sorting, but you can use the sort utility with the -h option . This option is needed to sort units of measure in a readable format:
du -h /var | sort -h
Findings
In this short article, we looked at the main features of the du command in Linux. As you can see, despite the fact that the utility is very simple, it allows you to see everything you need. What programs do you use to view the size of files and folders? Write in the comments!