Signup/Sign In

Du Command in Linux

du command

The "disk usage" command, also known as the du command, estimates the total amount of disk memory space consumed by input files and stored files in memory directories. It is used to discover and measure the memory utilization of specific files and directories that consume a significant amount of disk memory. When we provide the du the name of a file in a directory, it will estimate the disk's memory use in that directory.

We will study du Command in Linux in this chapter.

du command Syntax & Usage

The du command has the following basic syntax:

Syntax:

[OPTIONS]... [FILE]... 

When no FILE is given, du will automatically report the amount of disk RAM used in the current directory. When we run the command without any options, the memory utilization of the disk is reported in bytes of the supplied file or the memory usage of the directories where the file is placed and the subdirectories of that file.

du ~/Documents

The du command may accept multiple file and directory names as input:

.zsg /Documents/Pictures/.zsg /Documents/Pictures/.zsg /Documents/Pictures/.zsg

du will not work on a file or directory for which we do not have permission. Because the du command cannot read the manual, an error will be thrown. To execute such files and folders, we must add the keyword 'sudo' to the beginning of the command.

The following are some of the most often utilized du options:

The -a option tells du to display the disk memory utilization of individual files in that directory.

/Documents du -a

In essential words, the -h option is used to acquire the space use of a directory understandably.

For example, if we want to know the size of /Doc/lib and its subdirectories, we may use the following command:

/var sudo du -h

Because that directory is not accessible to people, sudo is needed.

Output:

... 6.0K /Doc/lib/apt/mirrors/partial /Doc/lib/apt/mirrors/partial /Doc/lib/apt/mirror
255M /Doc/lib/apt/mirrors 10.0K /Doc/lib/apt 2.7G /Doc/lib/apt

Another option, -s, is used to estimate the size of a given directory.

Use the -s option to omit the memory consumption of subdirectories from the output, which will only show the overall memory size of the provided directory.

du -sg /var 2.7G /var sudo du -sg /var sudo du -sg /var sudo du -

When we need to know the overall size of numerous directories, we may use the -c option, which returns the total sum of the directories' lengths.

total /Doc/log 3.9G /Doc/lib 5.4G sudo du -csg /Doc/log /Doc/lib 1.5G /Doc/log 3.9G /Doc/lib 5.4G

The max-depth option is used to get the memory use space of various levels of sub-directories. We may pass the story to the command to retrieve the disk space use of a given subfolder level.

In Linux, an example of the du command.
The following is an illustration of what I'm talking about:

/Doc/lib... 524K /Doc/lib/usbutils sudo du -h —max-depth=2
251M /Doc/lib/apt 3.5G /Doc/lib/acpi-support 4.5K /Doc/lib/acpi-support

–apparent size is an option that may estimate how much space or data is present within a specific file.

sudo du -sg —apparent-size /var/lib 2.5G sudo du -sg —apparent-size /var/lib 2.5G sudo du -sg —apparent-size /var/lib

du is a command that returns a list of all directories that begin with a particular letter or word. It's used to recognize the shell's pattern and deliver information based on it.

/P* 90M /home/linuxize/Pictures 152M /home/linuxize/Photos sudo du -csg
Total: 242 million

With the aid of pipes, the du command may combine with other commands. In Linux, pipes may be used to connect the du command to other commands.

To provide an example, if we want to display the top 4 big directories within the system, we may use the du output for sort command to sort directories according to their sizes, then pipe the output of that command to the top command, which will only show the top 4 directories.

head -4 sudo du -sg /var/ | sort -rsg
2.5G /var/lib/4.1G /var/lib/4.1G /var/lib/4.1G /var/lib/4.1G
1.5G /var/lib/pics/recent 2.8G /var/lib/pics/recent

Conclusion

The du commands on a UNIX or Linux system are generally used to assess a file's space use. We've gone through the various command parameters and examples of disk memory consumption outputs in a readable style, expressing and delivering the value of a directory size. We've also looked at the procedures for estimating the n-largest files and folders on a disk. We gained a decent idea of operating with the du command from these many command parameters.

The df tool can only provide information about the input file's disk space consumption inside a system. Still, the du command provides the estimated amount of disk memory space utilized by the specified directories or files. By executing the command man du on the terminal of our Linux system, we can acquire a list of all possible command parameters.