Linux DU Command with Examples

5

‘du’ stands for the disc usage in a system. It is used to identify the usage of space by the files present in the system that causes the disc storage space to run out of the provided space. The du command uses different options and flags to get used to the command and to know about the disk space left in your system.

If you want to have the summary of the usage of the disk storage of the current directory that is in use on your system, then you will use this keyword as a command on the terminal.

This command will provide the storage space of each directory. This output is very long as it contains approximately all the data containing files in it. To execute the space used of a single directory of your system then in addition to the ‘du’ keyword, you need to add a directory path with it. Here, we have used the Documents folder.

$ du /home/aqsayasin/documents

After mentioning the folder name, if you now want to see the storage size of a specific file format present in your system, you need to add that format name with the folder path.

$ du  /home/aqsayasin/*.txt

You can see that all the files are in the same file format. i.e. .txt because we have provided that format in the command.

Du and –h flag

Generally, the output of du is in the form of kilobytes. To make it easily usable and understandable by the user, we want to display the size in different formats i.e Kb, Mb, and Gb. This is also known to be the human-readable format. This is done by using the ‘-h’ flag with the ‘du’ command.

$ du –h /home/aqsayasin/data?.txt

We have used the file name ‘data?.txt’. Here, the ‘?’ means that all the files having names starting with data will be fetched by using this command.

The Total Size for Each Directory

To determine the total size of the whole directory instead of showing the sizes of the files present inside them.

$ du –s /home/aqsayasin
$ du –s

Both above-mentioned commands are the same in function. If you mention the directory or not, the results are the same.

The total size of the directory is displayed. Now, you will notice that the size is given in kilobytes as we have described earlier that by default the size is shown in kilobytes. But to make it in the human-readable format, we will use ‘-h’ with the ‘-s’ flag. So that the output is in other than kilobytes.

You can see that the output is now displayed in megabytes.

Disc Usage of Each Item

To know the disk usage of all the files and folders in a specific directory, we will use the ‘-a’ flag in the du command.

As the output contains all the files, it will be a very long result. We have shown a small screenshot for the user to make it easy for understanding. From the output, we come to know that the total size is of the whole folder in each line, not a single file.

We can also make the output precise by applying the limitations in the command. As in the given example, we have added the path of files to fetch the result of that specific file.  The ‘file?’ shows that all the files having names starting with file and having .txt extension are shown as an output.

$ -a –Sh /home/aqsayasin/file?.txt

The Total Size of the Disc

To obtain the total grand size of the disc used in the last session, we will use ‘c’ in the command. This will work in such a way that the file size will be shown and at the end. Total size as a whole will also be displayed. Here, we have two examples.

The first one will show the total of all the text files present in your system.

$ du –c –h /home/aqsayasin/*.txt

Whereas the second one will only show the total of files having names starting with data, present in a folder.

$ du –c –h /home/aqsayasin/data?.txt

File Timing of Last Modifications

The time of recently updated files can also be known easily. We can get the file size, its time of last modification, and the path of the files present in the relative folder. We want to get the output of all the files present in the folder having a name starting with data. We will use the –time command with the du command.

$ du –time –h /home/aqsayasin/data?.txt

The resultant screen shows the date of modification, and the time the file is opened and modified.

The du and the excluded term

If you want to print some data from the folder, but want to ignore or remove some data from your output, you can easily get your desired output. You desire to have all data except some unwanted files/data, then you can achieve this goal by using an excluded term in the du command. Consider all the text files present in your system by using any command that will fetch this sort of data.

Now from the above image, if we want to remove the last files. That is the file names starting with sample and we don’t want to get these file names in our output then we can customize this by using an excluded term.

$ du –ah –exclude=”sample?.txt” /home/aqsayasin/*.txt

The output doesn’t contain the sample starting files. The command contains the exclude initialization and then the file path in it.

Combine Major Flags in a Single Command

Now, we have to make usage of a command that includes more than one flag in it. –c, -s, -h are present in the command, which means that the output will contain the size of the folders having files and the total of all the folder size. We have taken two folders as input.

One thing to be mentioned here is that we have used the Sudo command. This means that the du command can be executed by using Sudo privileges.

$ sudo du –csh  /var/log  /var/lib

The output contains the size of both folders. One is in megabytes and the other one is in gigabytes. The total of both folders is also displayed as an output.

Another example of using 3 flags combined in a single command is that all the files of the data name will be displayed from the system and the subtotal is also mentioned below.

Display Apparent Size by Using du

The apparent size deals with the actual size of the file having data in it. This is done by using –apparent –size in the command like this s given below

$ sudo du –sh – -apparent-size /var/lib

The actual size of this file is shown in the output

Disk Information in Different Byte Units

Output in kilobytes
As described above, the size of any file is written in kilobytes by default. But you can still use –k in the command. Whereas, –d shows the depth of a file.

Output in megabytes
To get the output in megabytes, use –m in the command instead of –k

Conclusion

In this article, we have explained the usage of the du command in the Linux operating system. This command is easily understandable and known to be the human-readable format for the new user in Linux. So, using this disc space can easily be managed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More